home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / ld / dist / ld.c.bak2 < prev    next >
Encoding:
Text File  |  1990-07-26  |  138.5 KB  |  5,148 lines

  1. /* Linker `ld' for GNU
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by Richard Stallman with some help from Eric Albert.
  19.    Set, indirect, and warning symbol features added by Randy Smith.  */
  20.  
  21. #include <ar.h>
  22. #include <stdio.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <sys/file.h>
  26. #ifndef sony_news
  27. #include <fcntl.h>
  28. #endif
  29.  
  30. #define TARGET_SUN2             2
  31. #define TARGET_SUN3             3
  32. #define TARGET_SUN4             4
  33. #define TARGET_ALTOS            5
  34. #define TARGET_I386             6   
  35. #define TARGET_HPUX             7
  36. #define TARGET_SONY_NEWS        8
  37. #define TARGET_SEQUENT          9
  38. #define TARGET_VAX              10
  39.  
  40. /*
  41.  * If `TARGET_MACHINE' was not defined on the compiler command line,
  42.  * then set it equal to the host machine.
  43.  */
  44. #ifndef TARGET_MACHINE
  45. #if defined(sun) && defined(sparc)
  46. #define TARGET_MACHINE  TARGET_SUN4
  47. #endif
  48. #if defined(sun) && (defined(m68020) || defined(mc68020))
  49. #define TARGET_MACHINE  TARGET_SUN3
  50. #endif
  51. #if defined(sun) && (defined(m68010) || defined(mc68010))
  52. #define TARGET_MACHINE  TARGET_SUN2
  53. #endif
  54. #if defined(ALTOS)
  55. #define TARGET_MACHINE  TARGET_ALTOS
  56. #endif
  57. #if defined(hpux)
  58. #define TARGET_MACHINE  TARGET_HPUX
  59. #endif
  60. #if defined(is386)
  61. #define TARGET_MACHINE  TARGET_I386
  62. #endif
  63. #if defined(sony_news)
  64. #define TARGET_MACHINE  TARGET_SONY_NEWS
  65. #endif
  66. #if defined(sequent)
  67. #define TARGET_MACHINE  TARGET_SEQUENT
  68. #endif
  69. #if defined(is68k) && !defined(TARGET_MACHINE)
  70. #define TARGET_MACHINE  TARGET_68K
  71. #endif
  72. #endif
  73.  
  74. #ifndef TARGET_MACHINE
  75.     CANNOT COMPILE, NO TARGET MACHINE SPECIFIED
  76. #endif
  77.  
  78. #ifndef BIG_ENDIAN
  79. #define BIG_ENDIAN      4321
  80. #endif
  81. #ifndef LITTLE_ENDIAN
  82. #define LITTLE_ENDIAN   1234
  83. #endif
  84. #ifndef PDP_ENDIAN
  85. /* I think it is unlikely that anybody will try to cross compile
  86.  * to or from a pdp, so only big-endian and little-endian are supported. */
  87. #define PDP_ENDIAN   3412
  88. #endif
  89.  
  90. #if TARGET_MACHINE==TARGET_SUN4         || \
  91.     TARGET_MACHINE==TARGET_SUN3         || \
  92.     TARGET_MACHINE==TARGET_SUN2         || \
  93.     TARGET_MACHINE==TARGET_ALTOS        || \
  94.     TARGET_MACHINE==TARGET_HPUX         || \
  95.     TARGET_MACHINE==TARGET_SONY_NEWS    || \
  96.     TARGET_MACHINE==TARGET_68K
  97. #define TARGET_BYTE_ORDER       BIG_ENDIAN
  98. #endif
  99.  
  100. #if TARGET_MACHINE==TARGET_VAX          || \
  101.     TARGET_MACHINE==TARGET_I386
  102. #define TARGET_BYTE_ORDER       LITTLE_ENDIAN
  103. #endif
  104.  
  105. #if TARGET_MACHINE==TARGET_SUN3
  106. #define TARGET_PAGE_SIZE    0x2000
  107. #endif
  108.  
  109. #if defined(sparc)      || \
  110.     defined(m68000)     || \
  111.     defined(mc68000)    || \
  112.     defined(m68010)     || \
  113.     defined(mc68010)    || \
  114.     defined(m68020)     || \
  115.     defined(mc68020)    || \
  116.     defined(is68k)      || \
  117.     defined(hpux)       || \
  118.     defined(sony_news)
  119. #define HOST_BYTE_ORDER     BIG_ENDIAN
  120. #endif
  121.  
  122. #if defined(vax)        || \
  123.     defined(is386)      || \
  124.     defined(ds3100)     
  125. #define HOST_BYTE_ORDER     LITTLE_ENDIAN
  126. #endif
  127.  
  128. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  129. static void fix_byte_order();
  130. static void fix_exec_header_byte_order();
  131. static void fix_symbol_byte_order();
  132. static void target_to_host_reloc_byte_order();
  133. static void host_to_target_reloc_byte_order();
  134. static void fix_symbol_root_byte_order();
  135. #endif
  136.  
  137. #ifdef COFF_ENCAPSULATE
  138. #include "a.out.encap.h"
  139. #else
  140. #ifdef sprite
  141. #if TARGET_MACHINE==TARGET_SUN4
  142. #include <sun4.md/a.out.h>
  143. #elif TARGET_MACHINE==TARGET_SUN3
  144. #include <sun3.md/a.out.h>
  145. #elif TARGET_MACHINE==TARGET_SEQUENT
  146. #include <sym.md/a.out.h>
  147. #else
  148.     no a.out.h specified
  149. #endif
  150. #else
  151. #include <a.out.h>
  152. #endif
  153. #endif
  154.  
  155. #ifdef sprite
  156. #undef NEW_SEG_SIZE
  157. #if TARGET_MACHINE==TARGET_SUN4
  158. #define NEW_SEG_SIZE    0x40000
  159. #endif
  160. #if TARGET_MACHINE==TARGET_SUN3
  161. #define NEW_SEG_SIZE    0x20000
  162. #endif
  163. #endif  /* sprite */
  164.  
  165. #ifndef N_SET_MAGIC
  166. #define N_SET_MAGIC(exec, val)  ((exec).a_magic = val)
  167. #endif
  168.  
  169. /* If compiled with GNU C, use the built-in alloca */
  170. #ifdef __GNUC__
  171. #define alloca __builtin_alloca
  172. #endif
  173.  
  174. /* Always use the GNU version of debugging symbol type codes, if possible.  */
  175.  
  176. #include "stab.h"
  177. #define CORE_ADDR unsigned long    /* For symseg.h */
  178. #include "symseg.h"
  179.  
  180. #ifdef USG
  181. #include <string.h>
  182. #else
  183. #include <strings.h>
  184. #endif
  185.  
  186. /* Determine whether we should attempt to handle (minimally)
  187.    N_BINCL and N_EINCL.  */
  188.  
  189. #if defined (__GNU_STAB__) || defined (N_BINCL)
  190. #define HAVE_SUN_STABS
  191. #endif
  192.  
  193. #define min(a,b) ((a) < (b) ? (a) : (b))
  194.  
  195. /* Macro to control the number of undefined references printed */
  196. #define MAX_UREFS_PRINTED    10
  197.  
  198. /* Size of a page; obtained from the operating system.  */
  199.  
  200. int page_size;
  201.  
  202. /* Name this program was invoked by.  */
  203.  
  204. char *progname;
  205.  
  206. /* System dependencies */
  207.  
  208. /* Define this if names etext, edata and end should not start with `_'.  */
  209. /* #define nounderscore 1 */
  210.  
  211. /* Define NON_NATIVE if using BSD or pseudo-BSD file format on a system
  212.    whose native format is different.  */
  213. /* #define NON_NATIVE */
  214.  
  215. /* Define this to specify the default executable format.  */
  216.  
  217. #if TARGET_MACHINE==TARGET_HPUX
  218. #define DEFAULT_MAGIC NMAGIC  /* hpux bugs screw ZMAGIC */
  219. #endif
  220.  
  221. #ifndef DEFAULT_MAGIC
  222. #define DEFAULT_MAGIC ZMAGIC
  223. #endif
  224.  
  225. /* Ordinary 4.3bsd lacks these macros in a.out.h.  */
  226.  
  227. #ifndef N_TXTADDR
  228. #if TARGET_MACHINE==TARGET_VAX || TARGET_MACHINE==TARGET_SONY_NEWS
  229. #define N_TXTADDR(X) 0
  230. #endif
  231. #if TARGET_MACHINE==TARGET_SUN3 || TARGET_MACHINE==TARGET_SUN2
  232. #define N_TXTADDR(x)  (sizeof (struct exec))
  233. #endif
  234. #if TARGET_MACHINE==TARGET_SEQUENT
  235. #define    N_TXTADDR(x) (N_ADDRADJ(x))
  236. #endif
  237. #endif
  238.  
  239. #ifndef N_DATADDR
  240. #if TARGET_MACHINE==TARGET_VAX || TARGET_MACHINE==TARGET_SONY_NEWS
  241. #define N_DATADDR(x) \
  242.     (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  243.     : (page_size+((N_TXTADDR(x)+(x).a_text-1) & ~(page_size-1))))
  244. #endif
  245. #if TARGET_MACHINE==TARGET_SUN3 || TARGET_MACHINE==TARGET_SUN2
  246. #define SEGMENT_SIZE 0x20000
  247. #define N_DATADDR(x) \
  248.     (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  249.      : (SEGMENT_SIZE + ((N_TXTADDR(x)+(x).a_text-1) & ~(SEGMENT_SIZE-1))))
  250. #endif
  251. #if TARGET_MACHINE==TARGET_SEQUENT
  252. #define N_DATADDR(x) \
  253.     (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  254.     : (page_size+(((x).a_text-1) & ~(page_size-1))))
  255. #endif
  256. #endif
  257.  
  258. /* Define how to initialize system-dependent header fields.  */
  259. #if TARGET_MACHINE==TARGET_SUN4
  260. #define INITIALIZE_HEADER \
  261.   {outheader.a_machtype = M_SPARC; outheader.a_toolversion = 1;}
  262. #endif
  263. #if TARGET_MACHINE==TARGET_SUN2
  264. #define INITIALIZE_HEADER outheader.a_machtype = M_68010
  265. #endif
  266. #if TARGET_MACHINE==TARGET_SUN3
  267. #define INITIALIZE_HEADER outheader.a_machtype = M_68020
  268. #endif
  269. #if TARGET_MACHINE==TARGET_ALTOS
  270. #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_68020)
  271. #endif
  272. #if TARGET_MACHINE==TARGET_HPUX
  273. #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, HP9000S200_ID)
  274. #endif
  275. #if TARGET_MACHINE==TARGET_I386
  276. #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_386)
  277. #endif
  278. #if TARGET_MACHINE==TARGET_68K
  279. #define INITIALIZE_HEADER outheader.a_machtype = 0;  
  280. /* This enables code to take care of an ugly hack in the ISI OS.
  281.    If a symbol beings with _$, then the object file is included only
  282.    if the rest of the symbol name has been referenced. */
  283. #define DOLLAR_KLUDGE
  284. #endif
  285.  
  286. /*
  287.  * Alloca include.
  288.  */
  289. #if defined(sun) && defined(sparc) && !defined(__GNUC__)
  290. #include "alloca.h"
  291. #endif
  292.  
  293. #ifndef L_SET
  294. #define L_SET 0
  295. #endif
  296.  
  297. /*
  298.  * Ok.  Following are the relocation information macros.  If your
  299.  * system cannot use the default set (below), you must define all of these:
  300.  
  301.  *   relocation_info: This must be typedef'd (or #define'd) to the type
  302.  * of structure that is stored in the relocation info section of your
  303.  * a.out files.  Often this is defined in the a.out.h for your system.
  304.  *
  305.  *   RELOC_ADDRESS (rval): Offset into the current section of the
  306.  * <whatever> to be relocated.  *Must be an lvalue*.
  307.  *
  308.  *   RELOC_EXTERN_P (rval):  Is this relocation entry based on an
  309.  * external symbol (1), or was it fully resolved upon entering the
  310.  * loader (0) in which case some combination of the value in memory
  311.  * (if RELOC_MEMORY_ADD_P) and the extra (if RELOC_ADD_EXTRA) contains
  312.  * what the value of the relocation actually was.  *Must be an lvalue*.
  313.  *
  314.  *   RELOC_TYPE (rval): For a non-external relocation, this is the
  315.  * segment to relocate for.
  316.  *
  317.  *   RELOC_SYMBOL (rval): For an external relocation, this is the
  318.  * index of its symbol in the symbol table.  *Must be an lvalue*.
  319.  *
  320.  *   RELOC_MEMORY_ADD_P (rval): This should be 1 if the final
  321.  * relocation value output here should be added to memory; 0, if the
  322.  * section of memory described should simply be set to the relocation
  323.  * value.
  324.  *
  325.  *   RELOC_MEMORY_ADD_P (rval): If this is nonzero, the value previously
  326.  * present in the memory location to be relocated is *added*
  327.  * to the relocation value, to produce the final result.
  328.  * Otherwise, the relocation value is stored in the memory location
  329.  * and the value previously found there is ignored.
  330.  * By default, this is always 1.
  331.  *
  332.  *   RELOC_MEMORY_SUB_P (rval): If this is nonzero, the value previously
  333.  * present in the memory location to be relocated is *subtracted*
  334.  * from the relocation value, to produce the final result.
  335.  * By default, this is always 0.
  336.  *
  337.  *   RELOC_ADD_EXTRA (rval): (Optional) This macro, if defined, gives
  338.  * an extra value to be added to the relocation value based on the
  339.  * individual relocation entry.  *Must be an lvalue if defined*.
  340.  *
  341.  *   RELOC_PCREL_P (rval): True if the relocation value described is
  342.  * pc relative.
  343.  *
  344.  *   RELOC_VALUE_RIGHTSHIFT (rval): Number of bits right to shift the
  345.  * final relocation value before putting it where it belongs.
  346.  *
  347.  *   RELOC_TARGET_SIZE (rval): log to the base 2 of the number of
  348.  * bytes of size this relocation entry describes; 1 byte == 0; 2 bytes
  349.  * == 1; 4 bytes == 2, and etc.  This is somewhat redundant (we could
  350.  * do everything in terms of the bit operators below), but having this
  351.  * macro could end up producing better code on machines without fancy
  352.  * bit twiddling.  Also, it's easier to understand/code big/little
  353.  * endian distinctions with this macro.
  354.  *
  355.  *   RELOC_TARGET_BITPOS (rval): The starting bit position within the
  356.  * object described in RELOC_TARGET_SIZE in which the relocation value
  357.  * will go.
  358.  *
  359.  *   RELOC_TARGET_BITSIZE (rval): How many bits are to be replaced
  360.  * with the bits of the relocation value.  It may be assumed by the
  361.  * code that the relocation value will fit into this many bits.  This
  362.  * may be larger than RELOC_TARGET_SIZE if such be useful.
  363.  *
  364.  *
  365.  *        Things I haven't implemented
  366.  *        ----------------------------
  367.  *
  368.  *    Values for RELOC_TARGET_SIZE other than 0, 1, or 2.
  369.  *
  370.  *    Pc relative relocation for External references.
  371.  *
  372.  *
  373.  */
  374.  
  375. #if TARGET_MACHINE==TARGET_SUN4
  376. /* Sparc (Sun 4) macros */
  377. #undef relocation_info
  378. #define relocation_info                    reloc_info_sparc
  379. #define RELOC_ADDRESS(r)        ((r)->r_address)                 
  380. #define RELOC_EXTERN_P(r)               ((r)->r_extern)      
  381. #define RELOC_TYPE(r)                   ((r)->r_index)  
  382. #define RELOC_SYMBOL(r)                 ((r)->r_index)   
  383. #define RELOC_MEMORY_SUB_P(r)        0
  384. #define RELOC_MEMORY_ADD_P(r)           0
  385. #define RELOC_ADD_EXTRA(r)              ((r)->r_addend)       
  386. #define RELOC_PCREL_P(r)                \
  387.     ((int) (r)->r_type >= (int) RELOC_DISP8 && \
  388.      (int) (r)->r_type <= (int) RELOC_WDISP22)
  389. #define RELOC_VALUE_RIGHTSHIFT(r) \
  390.     (reloc_target_rightshift[(int) (r)->r_type])
  391. #define RELOC_TARGET_SIZE(r)            (reloc_target_size[(int) (r)->r_type])
  392. #define RELOC_TARGET_BITPOS(r)          0
  393. #define RELOC_TARGET_BITSIZE(r) \
  394.     (reloc_target_bitsize[(int) (r)->r_type])
  395.  
  396. /* Note that these are very dependent on the order of the enums in
  397.    enum reloc_type (in a.out.h); if they change the following must be
  398.    changed */
  399. /* Also note that the last few may be incorrect; I have no information */
  400. static int reloc_target_rightshift[] = {
  401.   0, 0, 0, 0, 0, 0, 2, 2, 10, 0, 0, 0, 0, 0, 0,
  402. };
  403. static int reloc_target_size[] = {
  404.   0, 1, 2, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  405. };
  406. static int reloc_target_bitsize[] = {
  407.   8, 16, 32, 8, 16, 32, 30, 22, 22, 22, 13, 10, 32, 32, 16,
  408. };
  409.  
  410. #define    MAX_ALIGNMENT    (sizeof (double))
  411.   /* The pagesize on an old sun4 is 0x2000, on sparcStation it is 0x1000.
  412.      If you want to use the same binaries on both, then you need to use
  413.      the larger pagesize. */
  414. #define TARGET_PAGE_SIZE    0x2000
  415. #endif
  416.  
  417. #if TARGET_MACHINE==TARGET_SEQUENT
  418. #define RELOC_ADDRESS(r)        ((r)->r_address)
  419. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  420. #define RELOC_TYPE(r)        ((r)->r_symbolnum)
  421. #define RELOC_SYMBOL(r)        ((r)->r_symbolnum)
  422. #define RELOC_MEMORY_SUB_P(r)    ((r)->r_bsr)
  423. #define RELOC_MEMORY_ADD_P(r)    1
  424. #undef RELOC_ADD_EXTRA
  425. #define RELOC_PCREL_P(r)        ((r)->r_pcrel || (r)->r_bsr)
  426. #define RELOC_VALUE_RIGHTSHIFT(r)    0
  427. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  428. #define RELOC_TARGET_BITPOS(r)    0
  429. #define RELOC_TARGET_BITSIZE(r)    32
  430. #endif
  431.  
  432. /* Default macros */
  433. #ifndef RELOC_ADDRESS
  434. #define RELOC_ADDRESS(r)        ((r)->r_address)
  435. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  436. #define RELOC_TYPE(r)        ((r)->r_symbolnum)
  437. #define RELOC_SYMBOL(r)        ((r)->r_symbolnum)
  438. #define RELOC_MEMORY_SUB_P(r)    0
  439. #define RELOC_MEMORY_ADD_P(r)    1
  440. #undef RELOC_ADD_EXTRA
  441. #define RELOC_PCREL_P(r)        ((r)->r_pcrel)
  442. #define RELOC_VALUE_RIGHTSHIFT(r)    0
  443. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  444. #define RELOC_TARGET_BITPOS(r)    0
  445. #define RELOC_TARGET_BITSIZE(r)    32
  446. #endif
  447.  
  448. #ifndef MAX_ALIGNMENT
  449. #define    MAX_ALIGNMENT    (sizeof (int))
  450. #endif
  451.  
  452. #ifdef nounderscore
  453. #define LPREFIX '.'
  454. #else
  455. #define LPREFIX 'L'
  456. #endif
  457.  
  458.  
  459. /* Special global symbol types understood by GNU LD.  */
  460.  
  461. /* The following type indicates the definition of a symbol as being
  462.    an indirect reference to another symbol.  The other symbol
  463.    appears as an undefined reference, immediately following this symbol.
  464.  
  465.    Indirection is asymmetrical.  The other symbol's value will be used
  466.    to satisfy requests for the indirect symbol, but not vice versa.
  467.    If the other symbol does not have a definition, libraries will
  468.    be searched to find a definition.
  469.  
  470.    So, for example, the following two lines placed in an assembler
  471.    input file would result in an object file which would direct gnu ld
  472.    to resolve all references to symbol "foo" as references to symbol
  473.    "bar". 
  474.  
  475.     .stabs "_foo",11,0,0,0
  476.     .stabs "_bar",1,0,0,0
  477.  
  478.    Note that (11 == (N_INDR | N_EXT)) and (1 == (N_UNDF | N_EXT)).  */
  479.  
  480. #ifndef N_INDR
  481. #define N_INDR 0xa
  482. #endif
  483.  
  484. /* The following symbols refer to set elements.  These are expected
  485.    only in input to the loader; they should not appear in loader
  486.    output (unless relocatable output is requested).  To be recognized
  487.    by the loader, the input symbols must have their N_EXT bit set.
  488.    All the N_SET[ATDB] symbols with the same name form one set.  The
  489.    loader collects all of these elements at load time and outputs a
  490.    vector for each name.
  491.    Space (an array of 32 bit words) is allocated for the set in the
  492.    data section, and the n_value field of each set element value is
  493.    stored into one word of the array.
  494.    The first word of the array is the length of the set (number of
  495.    elements).  The last word of the vector is set to zero for possible
  496.    use by incremental loaders.  The array is ordered by the linkage
  497.    order; the first symbols which the linker encounters will be first
  498.    in the array.
  499.  
  500.    In C syntax this looks like:
  501.  
  502.     struct set_vector {
  503.       unsigned int length;
  504.       unsigned int vector[length];
  505.       unsigned int always_zero;
  506.     };
  507.  
  508.    Before being placed into the array, each element is relocated
  509.    according to its type.  This allows the loader to create an array
  510.    of pointers to objects automatically.  N_SETA type symbols will not
  511.    be relocated.
  512.  
  513.    The address of the set is made into an N_SETV symbol
  514.    whose name is the same as the name of the set.
  515.    This symbol acts like a N_DATA global symbol
  516.    in that it can satisfy undefined external references.
  517.  
  518.    For the purposes of determining whether or not to load in a library
  519.    file, set element definitions are not considered "real
  520.    definitions"; they will not cause the loading of a library
  521.    member.
  522.  
  523.    If relocatable output is requested, none of this processing is
  524.    done.  The symbols are simply relocated and passed through to the
  525.    output file.
  526.  
  527.    So, for example, the following three lines of assembler code
  528.    (whether in one file or scattered between several different ones)
  529.    will produce a three element vector (total length is five words;
  530.    see above), referenced by the symbol "_xyzzy", which will have the
  531.    addresses of the routines _init1, _init2, and _init3.
  532.  
  533.    *NOTE*: If symbolic addresses are used in the n_value field of the
  534.    defining .stabs, those symbols must be defined in the same file as
  535.    that containing the .stabs.
  536.  
  537.     .stabs "_xyzzy",23,0,0,_init1
  538.     .stabs "_xyzzy",23,0,0,_init2
  539.     .stabs "_xyzzy",23,0,0,_init3
  540.  
  541.    Note that (23 == (N_SETT | N_EXT)).  */
  542.  
  543. #ifndef N_SETA
  544. #define    N_SETA    0x14        /* Absolute set element symbol */
  545. #endif                /* This is input to LD, in a .o file.  */
  546.  
  547. #ifndef N_SETT
  548. #define    N_SETT    0x16        /* Text set element symbol */
  549. #endif                /* This is input to LD, in a .o file.  */
  550.  
  551. #ifndef N_SETD
  552. #define    N_SETD    0x18        /* Data set element symbol */
  553. #endif                /* This is input to LD, in a .o file.  */
  554.  
  555. #ifndef N_SETB
  556. #define    N_SETB    0x1A        /* Bss set element symbol */
  557. #endif                /* This is input to LD, in a .o file.  */
  558.  
  559. /* Macros dealing with the set element symbols defined in a.out.h */
  560. #define    SET_ELEMENT_P(x)    ((x)>=N_SETA&&(x)<=(N_SETB|N_EXT))
  561. #define TYPE_OF_SET_ELEMENT(x)    ((x)-N_SETA+N_ABS)
  562.  
  563. #ifndef N_SETV
  564. #define N_SETV    0x1C        /* Pointer to set vector in data area.  */
  565. #endif                /* This is output from LD.  */
  566.  
  567. /* If a this type of symbol is encountered, its name is a warning
  568.    message to print each time the symbol referenced by the next symbol
  569.    table entry is referenced.   
  570.  
  571.    This feature may be used to allow backwards compatibility with
  572.    certain functions (eg. gets) but to discourage programmers from
  573.    their use.
  574.  
  575.    So if, for example, you wanted to have ld print a warning whenever
  576.    the function "gets" was used in their C program, you would add the
  577.    following to the assembler file in which gets is defined:
  578.  
  579.     .stabs "Obsolete function \"gets\" referenced",30,0,0,0
  580.     .stabs "_gets",1,0,0,0
  581.  
  582.    These .stabs do not necessarily have to be in the same file as the
  583.    gets function, they simply must exist somewhere in the compilation.  */
  584.  
  585. #ifndef N_WARNING
  586. #define N_WARNING 0x1E        /* Warning message to print if symbol
  587.                    included */
  588. #endif                /* This is input to ld */
  589.  
  590. #ifndef __GNU_STAB__
  591.  
  592. /* Line number for the data section.  This is to be used to describe
  593.    the source location of a variable declaration.  */
  594. #ifndef N_DSLINE
  595. #define N_DSLINE (N_SLINE+N_DATA-N_TEXT)
  596. #endif
  597.  
  598. /* Line number for the bss section.  This is to be used to describe
  599.    the source location of a variable declaration.  */
  600. #ifndef N_BSLINE
  601. #define N_BSLINE (N_SLINE+N_BSS-N_TEXT)
  602. #endif
  603.  
  604. #endif /* not __GNU_STAB__ */
  605.  
  606. /* Symbol table */
  607.  
  608. /* Global symbol data is recorded in these structures,
  609.    one for each global symbol.
  610.    They are found via hashing in 'symtab', which points to a vector of buckets.
  611.    Each bucket is a chain of these structures through the link field.  */
  612.  
  613. typedef
  614.   struct glosym
  615.     {
  616.       /* Pointer to next symbol in this symbol's hash bucket.  */
  617.       struct glosym *link;
  618.       /* Name of this symbol.  */
  619.       char *name;
  620.       /* Value of this symbol as a global symbol.  */
  621.       long value;
  622.       /* Chain of external 'nlist's in files for this symbol, both defs
  623.      and refs.  */
  624.       struct nlist *refs;
  625.       /* Any warning message that might be associated with this symbol
  626.          from an N_WARNING symbol encountered. */
  627.       char *warning;
  628.       /* Nonzero means definitions of this symbol as common have been seen,
  629.      and the value here is the largest size specified by any of them.  */
  630.       int max_common_size;
  631.       /* For relocatable_output, records the index of this global sym in the
  632.      symbol table to be written, with the first global sym given index 0.*/
  633.       int def_count;
  634.       /* Nonzero means a definition of this global symbol is known to exist.
  635.      Library members should not be loaded on its account.  */
  636.       char defined;
  637.       /* Nonzero means a reference to this global symbol has been seen
  638.      in a file that is surely being loaded.
  639.      A value higher than 1 is the n_type code for the symbol's
  640.      definition.  */
  641.       char referenced;
  642.       /* A count of the number of undefined references printed for a
  643.      specific symbol.  If a symbol is unresolved at the end of
  644.      digest_symbols (and the loading run is supposed to produce
  645.      relocatable output) do_file_warnings keeps track of how many
  646.      unresolved reference error messages have been printed for
  647.      each symbol here.  When the number hits MAX_UREFS_PRINTED,
  648.      messages stop. */
  649.       unsigned char undef_refs;
  650.       /* 1 means that this symbol has multiple definitions.  2 means
  651.          that it has multiple definitions, and some of them are set
  652.      elements, one of which has been printed out already.  */
  653.       unsigned char multiply_defined;
  654.       /* Nonzero means print a message at all refs or defs of this symbol */
  655.       char trace;
  656.     }
  657.   symbol;
  658.  
  659. /* Number of buckets in symbol hash table */
  660. #define    TABSIZE    1009
  661.  
  662. /* The symbol hash table: a vector of TABSIZE pointers to struct glosym. */
  663. symbol *symtab[TABSIZE];
  664.  
  665. /* Number of symbols in symbol hash table. */
  666. int num_hash_tab_syms = 0;
  667.  
  668. /* Count the number of nlist entries that are for local symbols.
  669.    This count and the three following counts
  670.    are incremented as as symbols are entered in the symbol table.  */
  671. int local_sym_count;
  672.  
  673. /* Count number of nlist entries that are for local symbols
  674.    whose names don't start with L. */
  675. int non_L_local_sym_count;
  676.  
  677. /* Count the number of nlist entries for debugger info.  */
  678. int debugger_sym_count;
  679.  
  680. /* Count the number of global symbols referenced and not defined.  */
  681. int undefined_global_sym_count;
  682.  
  683. /* Count the number of global symbols multiply defined.  */
  684. int multiple_def_count;
  685.  
  686. /* Count the number of defined global symbols.
  687.    Each symbol is counted only once
  688.    regardless of how many different nlist entries refer to it,
  689.    since the output file will need only one nlist entry for it.
  690.    This count is computed by `digest_symbols';
  691.    it is undefined while symbols are being loaded. */
  692. int defined_global_sym_count;
  693.  
  694. /* Count the number of symbols defined through common declarations.
  695.    This count is kept in symdef_library, linear_library, and
  696.    enter_global_ref.  It is incremented when the defined flag is set
  697.    in a symbol because of a common definition, and decremented when
  698.    the symbol is defined "for real" (ie. by something besides a common
  699.    definition).  */
  700. int common_defined_global_count;
  701.  
  702. /* Count the number of set element type symbols and the number of
  703.    separate vectors which these symbols will fit into.  See the
  704.    GNU a.out.h for more info.
  705.    This count is computed by 'enter_file_symbols' */
  706. int set_symbol_count;
  707. int set_vector_count;
  708.  
  709. /* Define a linked list of strings which define symbols which should
  710.    be treated as set elements even though they aren't.  Any symbol
  711.    with a prefix matching one of these should be treated as a set
  712.    element.
  713.  
  714.    This is to make up for deficiencies in many assemblers which aren't
  715.    willing to pass any stabs through to the loader which they don't
  716.    understand.  */
  717. struct string_list_element {
  718.   char *str;
  719.   struct string_list_element *next;
  720. };
  721.  
  722. struct string_list_element *set_element_prefixes;
  723.  
  724. /* Count the number of definitions done indirectly (ie. done relative
  725.    to the value of some other symbol. */
  726. int global_indirect_count;
  727.  
  728. /* Count the number of warning symbols encountered. */
  729. int warning_count;
  730.  
  731. /* Total number of symbols to be written in the output file.
  732.    Computed by digest_symbols from the variables above.  */
  733. int nsyms;
  734.  
  735.  
  736. /* Nonzero means ptr to symbol entry for symbol to use as start addr.
  737.    -e sets this.  */
  738. symbol *entry_symbol;
  739.  
  740. symbol *edata_symbol;   /* the symbol _edata */
  741. symbol *etext_symbol;   /* the symbol _etext */
  742. symbol *end_symbol;    /* the symbol _end */
  743.  
  744. /* Each input file, and each library member ("subfile") being loaded,
  745.    has a `file_entry' structure for it.
  746.  
  747.    For files specified by command args, these are contained in the vector
  748.    which `file_table' points to.
  749.  
  750.    For library members, they are dynamically allocated,
  751.    and chained through the `chain' field.
  752.    The chain is found in the `subfiles' field of the `file_entry'.
  753.    The `file_entry' objects for the members have `superfile' fields pointing
  754.    to the one for the library.  */
  755.  
  756. struct file_entry {
  757.   /* Name of this file.  */
  758.   char *filename;
  759.   /* Name to use for the symbol giving address of text start */
  760.   /* Usually the same as filename, but for a file spec'd with -l
  761.      this is the -l switch itself rather than the filename.  */
  762.   char *local_sym_name;
  763.  
  764.   /* Describe the layout of the contents of the file */
  765.  
  766.   /* The file's a.out header.  */
  767.   struct exec header;
  768.   /* Offset in file of GDB symbol segment, or 0 if there is none.  */
  769.   int symseg_offset;
  770.  
  771.   /* Describe data from the file loaded into core */
  772.  
  773.   /* Symbol table of the file.  */
  774.   struct nlist *symbols;
  775.   /* Size in bytes of string table.  */
  776.   int string_size;
  777.   /* Pointer to the string table.
  778.      The string table is not kept in core all the time,
  779.      but when it is in core, its address is here.  */
  780.   char *strings;
  781.  
  782.   /* Next two used only if `relocatable_output' or if needed for */
  783.   /* output of undefined reference line numbers. */
  784.  
  785.   /* Text reloc info saved by `write_text' for `coptxtrel'.  */
  786.   struct relocation_info *textrel;
  787.   /* Data reloc info saved by `write_data' for `copdatrel'.  */
  788.   struct relocation_info *datarel;
  789.  
  790.   /* Relation of this file's segments to the output file */
  791.  
  792.   /* Start of this file's text seg in the output file core image.  */
  793.   int text_start_address;
  794.   /* Start of this file's data seg in the output file core image.  */
  795.   int data_start_address;
  796.   /* Start of this file's bss seg in the output file core image.  */
  797.   int bss_start_address;
  798.   /* Offset in bytes in the output file symbol table
  799.      of the first local symbol for this file.  Set by `write_file_symbols'.  */
  800.   int local_syms_offset;
  801.  
  802.   /* For library members only */
  803.  
  804.   /* For a library, points to chain of entries for the library members.  */
  805.   struct file_entry *subfiles;
  806.   /* For a library member, offset of the member within the archive.
  807.      Zero for files that are not library members.  */
  808.   int starting_offset;
  809.   /* Size of contents of this file, if library member.  */
  810.   int total_size;
  811.   /* For library member, points to the library's own entry.  */
  812.   struct file_entry *superfile;
  813.   /* For library member, points to next entry for next member.  */
  814.   struct file_entry *chain;
  815.  
  816.   /* 1 if file is a library. */
  817.   char library_flag;
  818.  
  819.   /* 1 if file's header has been read into this structure.  */
  820.   char header_read_flag;
  821.  
  822.   /* 1 means search a set of directories for this file.  */
  823.   char search_dirs_flag;
  824.  
  825.   /* 1 means this is base file of incremental load.
  826.      Do not load this file's text or data.
  827.      Also default text_start to after this file's bss. */
  828.   char just_syms_flag;
  829. };
  830.  
  831. /* Vector of entries for input files specified by arguments.
  832.    These are all the input files except for members of specified libraries.  */
  833. struct file_entry *file_table;
  834.  
  835. /* Length of that vector.  */
  836. int number_of_files;
  837.  
  838. /* When loading the text and data, we can avoid doing a close
  839.    and another open between members of the same library.
  840.  
  841.    These two variables remember the file that is currently open.
  842.    Both are zero if no file is open.
  843.  
  844.    See `each_file' and `file_close'.  */
  845.  
  846. struct file_entry *input_file;
  847. int input_desc;
  848.  
  849. /* The name of the file to write; "a.out" by default.  */
  850.  
  851. char *output_filename;
  852.  
  853. /* Descriptor for writing that file with `mywrite'.  */
  854.  
  855. int outdesc;
  856.  
  857. /* Header for that file (filled in by `write_header').  */
  858.  
  859. struct exec outheader;
  860.  
  861. #ifdef COFF_ENCAPSULATE
  862. struct coffheader coffheader;
  863. int need_coff_header;
  864. #endif
  865.  
  866. /* The following are computed by `digest_symbols'.  */
  867.  
  868. int text_size;        /* total size of text of all input files.  */
  869. int data_size;        /* total size of data of all input files.  */
  870. int bss_size;        /* total size of bss of all input files.  */
  871. int text_reloc_size;    /* total size of text relocation of all input files.  */
  872. int data_reloc_size;    /* total size of data relocation of all input */
  873.             /* files.  */
  874.  
  875. /* Specifications of start and length of the area reserved at the end
  876.    of the text segment for the set vectors.  Computed in 'digest_symbols' */
  877. int set_sect_start;
  878. int set_sect_size;
  879.  
  880. /* Pointer for in core storage for the above vectors, before they are
  881.    written. */
  882. unsigned long *set_vectors;
  883.  
  884. /* Amount of cleared space to leave between the text and data segments.  */
  885.  
  886. int text_pad;
  887.  
  888. /* Amount of bss segment to include as part of the data segment.  */
  889.  
  890. int data_pad;
  891.  
  892. /* Format of __.SYMDEF:
  893.    First, a longword containing the size of the 'symdef' data that follows.
  894.    Second, zero or more 'symdef' structures.
  895.    Third, a longword containing the length of symbol name strings.
  896.    Fourth, zero or more symbol name strings (each followed by a null).  */
  897.  
  898. struct symdef {
  899.   int symbol_name_string_index;
  900.   int library_member_offset;
  901. };
  902.  
  903. /* Record most of the command options.  */
  904.  
  905. /* Address we assume the text section will be loaded at.
  906.    We relocate symbols and text and data for this, but we do not
  907.    write any padding in the output file for it.  */
  908. int text_start;
  909.  
  910. /* Offset of default entry-pc within the text section.  */
  911. int entry_offset;
  912.  
  913. /* Address we decide the data section will be loaded at.  */
  914. int data_start;
  915.  
  916. /* `text-start' address is normally this much plus a page boundary.
  917.    This is not a user option; it is fixed for each system.  */
  918. int text_start_alignment;
  919.  
  920. /* Nonzero if -T was specified in the command line.
  921.    This prevents text_start from being set later to default values.  */
  922. int T_flag_specified;
  923.  
  924. /* Nonzero if -Tdata was specified in the command line.
  925.    This prevents data_start from being set later to default values.  */
  926. int Tdata_flag_specified;
  927.  
  928. /* Size to pad data section up to.
  929.    We simply increase the size of the data section, padding with zeros,
  930.    and reduce the size of the bss section to match.  */
  931. int specified_data_size;
  932.  
  933. /* Magic number to use for the output file, set by switch.  */
  934. int magic;
  935.  
  936. /* Nonzero means print names of input files as processed.  */
  937. int trace_files;
  938.  
  939. /* Which symbols should be stripped (omitted from the output):
  940.    none, all, or debugger symbols.  */
  941. enum { STRIP_NONE, STRIP_ALL, STRIP_DEBUGGER } strip_symbols;
  942.  
  943. /* Which local symbols should be omitted:
  944.    none, all, or those starting with L.
  945.    This is irrelevant if STRIP_NONE.  */
  946. enum { DISCARD_NONE, DISCARD_ALL, DISCARD_L } discard_locals;
  947.  
  948. /* 1 => write load map.  */
  949. int write_map;
  950.  
  951. /* 1 => write relocation into output file so can re-input it later.  */
  952. int relocatable_output;
  953.  
  954. /* 1 => assign space to common symbols even if `relocatable_output'.  */
  955. int force_common_definition;
  956.  
  957. /* Standard directories to search for files specified by -l.  */
  958. char *standard_search_dirs[] =
  959. #ifdef STANDARD_SEARCH_DIRS
  960.   {STANDARD_SEARCH_DIRS};
  961. #else
  962. #ifdef NON_NATIVE
  963.   {"/usr/local/lib/gnu"};
  964. #else
  965.   {"/lib", "/usr/lib", "/usr/local/lib"};
  966. #endif
  967. #endif
  968.  
  969. /* Actual vector of directories to search;
  970.    this contains those specified with -L plus the standard ones.  */
  971. char **search_dirs;
  972.  
  973. /* Length of the vector `search_dirs'.  */
  974. int n_search_dirs;
  975.  
  976. /* Non zero means to create the output executable. */
  977. /* Cleared by nonfatal errors.  */
  978. int make_executable;
  979.  
  980. /* Force the executable to be output, even if there are non-fatal
  981.    errors */
  982. int force_executable;
  983.  
  984. /* Keep a list of any symbols referenced from the command line (so
  985.    that error messages for these guys can be generated). This list is
  986.    zero terminated. */
  987. struct glosym **cmdline_references;
  988. int cl_refs_allocated;
  989.  
  990. void bcopy (), bzero ();
  991. int malloc (), realloc ();
  992. #ifndef alloca
  993. int alloca ();
  994. #endif
  995. int free ();
  996.  
  997. int xmalloc ();
  998. int xrealloc ();
  999. void fatal ();
  1000. void fatal_with_file ();
  1001. void perror_name ();
  1002. void perror_file ();
  1003. void error ();
  1004.  
  1005. void digest_symbols ();
  1006. void print_symbols ();
  1007. void load_symbols ();
  1008. void decode_command ();
  1009. void list_undefined_symbols ();
  1010. void list_unresolved_references ();
  1011. void write_output ();
  1012. void write_header ();
  1013. void write_text ();
  1014. void read_file_relocation ();
  1015. void write_data ();
  1016. void write_rel ();
  1017. void write_syms ();
  1018. void write_symsegs ();
  1019. void mywrite ();
  1020. void symtab_init ();
  1021. void padfile ();
  1022. char *concat ();
  1023. char *get_file_name ();
  1024. symbol *getsym (), *getsym_soft ();
  1025.  
  1026. int
  1027. main (argc, argv)
  1028.      char **argv;
  1029.      int argc;
  1030. {
  1031.  
  1032. #ifdef TARGET_PAGE_SIZE
  1033.   page_size = TARGET_PAGE_SIZE;
  1034. #else
  1035.   page_size = getpagesize ();
  1036. #endif
  1037.  
  1038.   progname = argv[0];
  1039.  
  1040.   /* Clear the cumulative info on the output file.  */
  1041.   text_size = 0;
  1042.   data_size = 0;
  1043.   bss_size = 0;
  1044.   text_reloc_size = 0;
  1045.   data_reloc_size = 0;
  1046.  
  1047.   data_pad = 0;
  1048.   text_pad = 0;
  1049.  
  1050.   /* Initialize the data about options.  */
  1051.  
  1052.   specified_data_size = 0;
  1053.   strip_symbols = STRIP_NONE;
  1054.   trace_files = 0;
  1055.   discard_locals = DISCARD_NONE;
  1056.   entry_symbol = 0;
  1057.   write_map = 0;
  1058.   relocatable_output = 0;
  1059.   force_common_definition = 0;
  1060.   T_flag_specified = 0;
  1061.   Tdata_flag_specified = 0;
  1062.   magic = DEFAULT_MAGIC;
  1063.   make_executable = 1;
  1064.   force_executable = 0;
  1065.   set_element_prefixes = 0;
  1066.  
  1067.   /* Initialize the cumulative counts of symbols.  */
  1068.  
  1069.   local_sym_count = 0;
  1070.   non_L_local_sym_count = 0;
  1071.   debugger_sym_count = 0;
  1072.   undefined_global_sym_count = 0;
  1073.   set_symbol_count = 0;
  1074.   set_vector_count = 0;
  1075.   global_indirect_count = 0;
  1076.   warning_count = 0;
  1077.   multiple_def_count = 0;
  1078.   common_defined_global_count = 0;
  1079.  
  1080.   /* Keep a list of symbols referenced from the command line */
  1081.   cl_refs_allocated = 10;
  1082.   cmdline_references
  1083.     = (struct glosym **) xmalloc (cl_refs_allocated
  1084.                   * sizeof(struct glosym *));
  1085.   *cmdline_references = 0;
  1086.  
  1087.   /* Completely decode ARGV.  */
  1088.  
  1089.   decode_command (argc, argv);
  1090.  
  1091.   /* Create the symbols `etext', `edata' and `end'.  */
  1092.  
  1093.   if (!relocatable_output)
  1094.     symtab_init ();
  1095.  
  1096.   /* Determine whether to count the header as part of
  1097.      the text size, and initialize the text size accordingly.
  1098.      This depends on the kind of system and on the output format selected.  */
  1099.  
  1100.   N_SET_MAGIC (outheader, magic);
  1101. #ifdef INITIALIZE_HEADER
  1102.   INITIALIZE_HEADER;
  1103. #endif
  1104.  
  1105.   text_size = sizeof (struct exec);
  1106. #ifdef COFF_ENCAPSULATE
  1107.   if (relocatable_output == 0)
  1108.     {
  1109.       need_coff_header = 1;
  1110.       /* set this flag now, since it will change the values of N_TXTOFF, etc */
  1111.       N_SET_FLAGS (outheader, N_FLAGS_COFF_ENCAPSULATE);
  1112.       text_size += sizeof (struct coffheader);
  1113.     }
  1114. #endif
  1115.  
  1116.   text_size -= N_TXTOFF (outheader);
  1117.  
  1118.   if (text_size < 0)
  1119.     text_size = 0;
  1120.   entry_offset = text_size;
  1121.  
  1122.   if (!T_flag_specified && !relocatable_output)
  1123.     text_start = N_TXTADDR (outheader);
  1124.  
  1125.   /* The text-start address is normally this far past a page boundary.  */
  1126.   text_start_alignment = text_start % page_size;
  1127.  
  1128.   /* Load symbols of all input files.
  1129.      Also search all libraries and decide which library members to load.  */
  1130.  
  1131.   load_symbols ();
  1132.  
  1133.   /* Compute where each file's sections go, and relocate symbols.  */
  1134.  
  1135.   digest_symbols ();
  1136.  
  1137.   /* Print error messages for any missing symbols, for any warning
  1138.      symbols, and possibly multiple definitions */
  1139.  
  1140.   do_warnings (stderr);
  1141.  
  1142.   /* Print a map, if requested.  */
  1143.  
  1144.   if (write_map) print_symbols (stdout);
  1145.  
  1146.   /* Write the output file.  */
  1147.  
  1148.   if (make_executable || force_executable)
  1149.     write_output ();
  1150.  
  1151.   exit (!make_executable);
  1152. }
  1153.  
  1154. void decode_option ();
  1155.  
  1156. /* Analyze a command line argument.
  1157.    Return 0 if the argument is a filename.
  1158.    Return 1 if the argument is a option complete in itself.
  1159.    Return 2 if the argument is a option which uses an argument.
  1160.  
  1161.    Thus, the value is the number of consecutive arguments
  1162.    that are part of options.  */
  1163.  
  1164. int
  1165. classify_arg (arg)
  1166.      register char *arg;
  1167. {
  1168.   if (*arg != '-') return 0;
  1169.   switch (arg[1])
  1170.     {
  1171.     case 'A':
  1172.     case 'D':
  1173.     case 'e':
  1174.     case 'L':
  1175.     case 'l':
  1176.     case 'o':
  1177.     case 'u':
  1178.     case 'V':
  1179.     case 'y':
  1180.       if (arg[2])
  1181.     return 1;
  1182.       return 2;
  1183.  
  1184.     case 'B':
  1185.       if (! strcmp (&arg[2], "static"))
  1186.     return 1;
  1187.  
  1188.     case 'T':
  1189.       if (arg[2] == 0)
  1190.     return 2;
  1191.       if (! strcmp (&arg[2], "text"))
  1192.     return 2;
  1193.       if (! strcmp (&arg[2], "data"))
  1194.     return 2;
  1195.       return 1;
  1196.     }
  1197.  
  1198.   return 1;
  1199. }
  1200.  
  1201. /* Process the command arguments,
  1202.    setting up file_table with an entry for each input file,
  1203.    and setting variables according to the options.  */
  1204.  
  1205. void
  1206. decode_command (argc, argv)
  1207.      char **argv;
  1208.      int argc;
  1209. {
  1210.   register int i;
  1211.   register struct file_entry *p;
  1212.  
  1213.   number_of_files = 0;
  1214.   output_filename = "a.out";
  1215.  
  1216.   n_search_dirs = 0;
  1217.   search_dirs = (char **) xmalloc (sizeof (char *));
  1218.  
  1219.   /* First compute number_of_files so we know how long to make file_table.  */
  1220.   /* Also process most options completely.  */
  1221.  
  1222.   for (i = 1; i < argc; i++)
  1223.     {
  1224.       register int code = classify_arg (argv[i]);
  1225.       if (code)
  1226.     {
  1227.       if (i + code > argc)
  1228.         fatal ("no argument following %s\n", argv[i]);
  1229.  
  1230.       decode_option (argv[i], argv[i+1]);
  1231.  
  1232.       if (argv[i][1] == 'l' || argv[i][1] == 'A')
  1233.         number_of_files++;
  1234.  
  1235.       i += code - 1;
  1236.     }
  1237.       else
  1238.     number_of_files++;
  1239.     }
  1240.  
  1241.   if (!number_of_files)
  1242.     fatal ("no input files", 0);
  1243.  
  1244.   p = file_table
  1245.     = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
  1246.   bzero (p, number_of_files * sizeof (struct file_entry));
  1247.  
  1248.   /* Now scan again and fill in file_table.  */
  1249.   /* All options except -A and -l are ignored here.  */
  1250.  
  1251.   for (i = 1; i < argc; i++)
  1252.     {
  1253.       register int code = classify_arg (argv[i]);
  1254.  
  1255.       if (code)
  1256.     {
  1257.       char *string;
  1258.       if (code == 2)
  1259.         string = argv[i+1];
  1260.       else
  1261.         string = &argv[i][2];
  1262.  
  1263.       if (argv[i][1] == 'A')
  1264.         {
  1265.           if (p != file_table)
  1266.         fatal ("-A specified before an input file other than the first");
  1267.  
  1268.           p->filename = string;
  1269.           p->local_sym_name = string;
  1270.           p->just_syms_flag = 1;
  1271.           p++;
  1272.         }
  1273.       if (argv[i][1] == 'l')
  1274.         {
  1275.           p->filename = concat ("lib", string, ".a");
  1276.           p->local_sym_name = concat ("-l", string, "");
  1277.           p->search_dirs_flag = 1;
  1278.           p++;
  1279.         }
  1280.       i += code - 1;
  1281.     }
  1282.       else
  1283.     {
  1284.       p->filename = argv[i];
  1285.       p->local_sym_name = argv[i];
  1286.       p++;
  1287.     }
  1288.     }
  1289.  
  1290.   /* Now check some option settings for consistency.  */
  1291.  
  1292. #ifndef sprite
  1293. #ifdef NMAGIC
  1294.   if ((magic == ZMAGIC || magic == NMAGIC)
  1295. #else
  1296.   if ((magic == ZMAGIC)
  1297. #endif
  1298.       && (text_start - text_start_alignment) & (page_size - 1))
  1299.     fatal ("-T argument not multiple of page size, with sharable output", 0);
  1300. #endif
  1301.  
  1302.   /* Append the standard search directories to the user-specified ones.  */
  1303.   {
  1304.     int n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
  1305.     n_search_dirs += n;
  1306.     search_dirs
  1307.       = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  1308.     bcopy (standard_search_dirs, &search_dirs[n_search_dirs - n],
  1309.        n * sizeof (char *));
  1310.   }
  1311. }
  1312.  
  1313.  
  1314. void
  1315. add_cmdline_ref (sp)
  1316.      struct glosym *sp;
  1317. {
  1318.   struct glosym **ptr;
  1319.  
  1320.   for (ptr = cmdline_references;
  1321.        ptr < cmdline_references + cl_refs_allocated && *ptr;
  1322.        ptr++)
  1323.     ;
  1324.  
  1325.   if (ptr >= cmdline_references + cl_refs_allocated - 1)
  1326.     {
  1327.       int diff = ptr - cmdline_references;
  1328.       
  1329.       cl_refs_allocated *= 2;
  1330.       cmdline_references = (struct glosym **)
  1331.     xrealloc (cmdline_references,
  1332.          cl_refs_allocated * sizeof (struct glosym *));
  1333.       ptr = cmdline_references + diff;
  1334.     }
  1335.   
  1336.   *ptr++ = sp;
  1337.   *ptr = (struct glosym *) 0;
  1338. }
  1339.     
  1340. int
  1341. set_element_prefixed_p (name)
  1342.      char *name;
  1343. {
  1344.   struct string_list_element *p;
  1345.   int i;
  1346.  
  1347.   for (p = set_element_prefixes; p; p = p->next)
  1348.     {
  1349.       for (i = 0; p->str[i] != '\0' && (p->str[i] == name[i]); i++)
  1350.     ;
  1351.  
  1352.       if (p->str[i] == '\0')
  1353.     return 1;
  1354.     }
  1355.   return 0;
  1356. }
  1357.  
  1358. int parse ();
  1359.  
  1360. /* Record an option and arrange to act on it later.
  1361.    ARG should be the following command argument,
  1362.    which may or may not be used by this option.
  1363.  
  1364.    The `l' and `A' options are ignored here since they actually
  1365.    specify input files.  */
  1366.  
  1367. void
  1368. decode_option (swt, arg)
  1369.      register char *swt, *arg;
  1370. {
  1371.   /* We get Bstatic from gcc on suns.  */
  1372.   if (! strcmp (swt + 1, "Bstatic"))
  1373.     return;
  1374.   if (! strcmp (swt + 1, "Ttext"))
  1375.     {
  1376. #ifdef sprite 
  1377.       magic = OMAGIC;
  1378. #endif
  1379.       text_start = parse (arg, "%x", "invalid argument to -Ttext");
  1380.       T_flag_specified = 1;
  1381.       return;
  1382.     }
  1383.   if (! strcmp (swt + 1, "Tdata"))
  1384.     {
  1385.       data_start = parse (arg, "%x", "invalid argument to -Tdata");
  1386.       Tdata_flag_specified = 1;
  1387.       return;
  1388.     }
  1389.   if (! strcmp (swt + 1, "noinhibit-exec"))
  1390.     {
  1391.       force_executable = 1;
  1392.       return;
  1393.     }
  1394.  
  1395.   if (swt[2] != 0)
  1396.     arg = &swt[2];
  1397.  
  1398.   switch (swt[1])
  1399.     {
  1400.     case 'A':
  1401.       return;
  1402.  
  1403.     case 'D':
  1404.       specified_data_size = parse (arg, "%x", "invalid argument to -D");
  1405.       return;
  1406.  
  1407.     case 'd':
  1408.       force_common_definition = 1;
  1409.       return;
  1410.  
  1411.     case 'e':
  1412.       entry_symbol = getsym (arg);
  1413.       if (!entry_symbol->defined && !entry_symbol->referenced)
  1414.     undefined_global_sym_count++;
  1415.       entry_symbol->referenced = 1;
  1416.       add_cmdline_ref (entry_symbol);
  1417.       return;
  1418.  
  1419.     case 'l':
  1420.       return;
  1421.  
  1422.     case 'L':
  1423.       n_search_dirs++;
  1424.       search_dirs
  1425.     = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  1426.       search_dirs[n_search_dirs - 1] = arg;
  1427.       return;
  1428.  
  1429.     case 'M':
  1430.       write_map = 1;
  1431.       return;
  1432.  
  1433.     case 'N':
  1434.       magic = OMAGIC;
  1435.       return;
  1436.  
  1437. #ifdef NMAGIC
  1438.     case 'n':
  1439.       magic = NMAGIC;
  1440.       return;
  1441. #endif
  1442.  
  1443.     case 'o':
  1444.       output_filename = arg;
  1445.       return;
  1446.  
  1447.     case 'r':
  1448.       relocatable_output = 1;
  1449.       magic = OMAGIC;
  1450.       text_start = 0;
  1451.       return;
  1452.  
  1453.     case 'S':
  1454.       strip_symbols = STRIP_DEBUGGER;
  1455.       return;
  1456.  
  1457.     case 's':
  1458.       strip_symbols = STRIP_ALL;
  1459.       return;
  1460.  
  1461.     case 'T':
  1462. #ifdef sprite
  1463.       magic = OMAGIC;
  1464. #endif      
  1465.       text_start = parse (arg, "%x", "invalid argument to -T");
  1466.       T_flag_specified = 1;
  1467.       return;
  1468.  
  1469.     case 't':
  1470.       trace_files = 1;
  1471.       return;
  1472.  
  1473.     case 'u':
  1474.       {
  1475.     register symbol *sp = getsym (arg);
  1476.     if (!sp->defined && !sp->referenced)
  1477.       undefined_global_sym_count++;
  1478.     sp->referenced = 1;
  1479.     add_cmdline_ref (sp);
  1480.       }
  1481.       return;
  1482.  
  1483.     case 'V':
  1484.       {
  1485.     struct string_list_element *new
  1486.       = (struct string_list_element *)
  1487.         xmalloc (sizeof (struct string_list_element));
  1488.  
  1489.     new->str = arg;
  1490.     new->next = set_element_prefixes;
  1491.     set_element_prefixes = new;
  1492.     return;
  1493.       }
  1494.  
  1495.     case 'X':
  1496.       discard_locals = DISCARD_L;
  1497.       return;
  1498.  
  1499.     case 'x':
  1500.       discard_locals = DISCARD_ALL;
  1501.       return;
  1502.  
  1503.     case 'y':
  1504.       {
  1505.     register symbol *sp = getsym (&swt[2]);
  1506.     sp->trace = 1;
  1507.       }
  1508.       return;
  1509.  
  1510.     case 'z':
  1511.       magic = ZMAGIC;
  1512.       return;
  1513.  
  1514.     default:
  1515.       fatal ("invalid command option `%s'", swt);
  1516.     }
  1517. }
  1518.  
  1519. /** Convenient functions for operating on one or all files being */
  1520.  /** loaded.  */
  1521. void print_file_name ();
  1522.  
  1523. /* Call FUNCTION on each input file entry.
  1524.    Do not call for entries for libraries;
  1525.    instead, call once for each library member that is being loaded.
  1526.  
  1527.    FUNCTION receives two arguments: the entry, and ARG.  */
  1528.  
  1529. void
  1530. each_file (function, arg)
  1531.      register void (*function)();
  1532.      register int arg;
  1533. {
  1534.   register int i;
  1535.  
  1536.   for (i = 0; i < number_of_files; i++)
  1537.     {
  1538.       register struct file_entry *entry = &file_table[i];
  1539.       if (entry->library_flag)
  1540.         {
  1541.       register struct file_entry *subentry = entry->subfiles;
  1542.       for (; subentry; subentry = subentry->chain)
  1543.         (*function) (subentry, arg);
  1544.     }
  1545.       else
  1546.     (*function) (entry, arg);
  1547.     }
  1548. }
  1549.  
  1550. /* Call FUNCTION on each input file entry until it returns a non-zero
  1551.    value.  Return this value.
  1552.    Do not call for entries for libraries;
  1553.    instead, call once for each library member that is being loaded.
  1554.  
  1555.    FUNCTION receives two arguments: the entry, and ARG.  It must be a
  1556.    function returning unsigned long (though this can probably be fudged). */
  1557. #if 0
  1558. unsigned long
  1559. check_each_file (function, arg)
  1560.      register unsigned long (*function)();
  1561.      register int arg;
  1562. {
  1563.   register int i;
  1564.   register unsigned long return_val;
  1565.  
  1566.   for (i = 0; i < number_of_files; i++)
  1567.     {
  1568.       register struct file_entry *entry = &file_table[i];
  1569.       if (entry->library_flag)
  1570.         {
  1571.       register struct file_entry *subentry = entry->subfiles;
  1572.       for (; subentry; subentry = subentry->chain)
  1573.         if (return_val = (*function) (subentry, arg))
  1574.           return return_val;
  1575.     }
  1576.       else
  1577.     if (return_val = (*function) (entry, arg))
  1578.       return return_val;
  1579.     }
  1580.   return 0;
  1581. }
  1582. #endif
  1583.  
  1584. /* Like `each_file' but ignore files that were just for symbol definitions.  */
  1585.  
  1586. void
  1587. each_full_file (function, arg)
  1588.      register void (*function)();
  1589.      register int arg;
  1590. {
  1591.   register int i;
  1592.  
  1593.   for (i = 0; i < number_of_files; i++)
  1594.     {
  1595.       register struct file_entry *entry = &file_table[i];
  1596.       if (entry->just_syms_flag)
  1597.     continue;
  1598.       if (entry->library_flag)
  1599.         {
  1600.       register struct file_entry *subentry = entry->subfiles;
  1601.       for (; subentry; subentry = subentry->chain)
  1602.         (*function) (subentry, arg);
  1603.     }
  1604.       else
  1605.     (*function) (entry, arg);
  1606.     }
  1607. }
  1608.  
  1609. /* Close the input file that is now open.  */
  1610.  
  1611. void
  1612. file_close ()
  1613. {
  1614.   close (input_desc);
  1615.   input_desc = 0;
  1616.   input_file = 0;
  1617. }
  1618.  
  1619. /* Open the input file specified by 'entry', and return a descriptor.
  1620.    The open file is remembered; if the same file is opened twice in a row,
  1621.    a new open is not actually done.  */
  1622.  
  1623. int
  1624. file_open (entry)
  1625.      register struct file_entry *entry;
  1626. {
  1627.   register int desc;
  1628.  
  1629.   if (entry->superfile)
  1630.     return file_open (entry->superfile);
  1631.  
  1632.   if (entry == input_file)
  1633.     return input_desc;
  1634.  
  1635.   if (input_file) file_close ();
  1636.  
  1637.   if (entry->search_dirs_flag)
  1638.     {
  1639.       register char **p = search_dirs;
  1640.       int i;
  1641.  
  1642.       for (i = 0; i < n_search_dirs; i++)
  1643.     {
  1644.       register char *string
  1645.         = concat (search_dirs[i], "/", entry->filename);
  1646.       desc = open (string, O_RDONLY, 0);
  1647.       if (desc > 0)
  1648.         {
  1649.           entry->filename = string;
  1650.           entry->search_dirs_flag = 0;
  1651.           break;
  1652.         }
  1653.       free (string);
  1654.     }
  1655.     }
  1656.   else
  1657.     desc = open (entry->filename, O_RDONLY, 0);
  1658.  
  1659.   if (desc > 0)
  1660.     {
  1661.       input_file = entry;
  1662.       input_desc = desc;
  1663.       return desc;
  1664.     }
  1665.  
  1666.   perror_file (entry);
  1667.   /* NOTREACHED */
  1668. }
  1669.  
  1670. /* Print the filename of ENTRY on OUTFILE (a stdio stream),
  1671.    and then a newline.  */
  1672.  
  1673. void
  1674. prline_file_name (entry, outfile)
  1675.      struct file_entry *entry;
  1676.      FILE *outfile;
  1677. {
  1678.   print_file_name (entry, outfile);
  1679.   fprintf (outfile, "\n");
  1680. }
  1681.  
  1682. /* Print the filename of ENTRY on OUTFILE (a stdio stream).  */
  1683.  
  1684. void
  1685. print_file_name (entry, outfile)
  1686.      struct file_entry *entry;
  1687.      FILE *outfile;
  1688. {
  1689.   if (entry->superfile)
  1690.     {
  1691.       print_file_name (entry->superfile, outfile);
  1692.       fprintf (outfile, "(%s)", entry->filename);
  1693.     }
  1694.   else
  1695.     fprintf (outfile, "%s", entry->filename);
  1696. }
  1697.  
  1698. /* Return the filename of entry as a string (malloc'd for the purpose) */
  1699.  
  1700. char *
  1701. get_file_name (entry)
  1702.      struct file_entry *entry;
  1703. {
  1704.   char *result, *supfile;
  1705.   if (entry->superfile)
  1706.     {
  1707.       supfile = get_file_name (entry->superfile);
  1708.       result = (char *) xmalloc (strlen (supfile)
  1709.                  + strlen (entry->filename) + 3);
  1710.       sprintf (result, "%s(%s)", supfile, entry->filename);
  1711.       free (supfile);
  1712.     }
  1713.   else
  1714.     {
  1715.       result = (char *) xmalloc (strlen (entry->filename) + 1);
  1716.       strcpy (result, entry->filename);
  1717.     }
  1718.   return result;
  1719. }
  1720.  
  1721. /* Medium-level input routines for rel files.  */
  1722.  
  1723. /* Read a file's header into the proper place in the file_entry.
  1724.    DESC is the descriptor on which the file is open.
  1725.    ENTRY is the file's entry.  */
  1726.  
  1727. void
  1728. read_header (desc, entry)
  1729.      int desc;
  1730.      register struct file_entry *entry;
  1731. {
  1732.   register int len;
  1733.   struct exec *loc = (struct exec *) &entry->header;
  1734.  
  1735.   lseek (desc, entry->starting_offset, 0);
  1736.   len = read (desc, loc, sizeof (struct exec));
  1737.   if (len != sizeof (struct exec))
  1738.     fatal_with_file ("failure reading header of ", entry);
  1739. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  1740.   fix_exec_header_byte_order(loc);
  1741. #endif
  1742.   if (N_BADMAG (*loc))
  1743.     fatal_with_file ("bad magic number in ", entry);
  1744.  
  1745.   entry->header_read_flag = 1;
  1746. }
  1747.  
  1748. /* Read the symbols of file ENTRY into core.
  1749.    Assume it is already open, on descriptor DESC.
  1750.    Also read the length of the string table, which follows the symbol table,
  1751.    but don't read the contents of the string table.  */
  1752.  
  1753. void
  1754. read_entry_symbols (desc, entry)
  1755.      struct file_entry *entry;
  1756.      int desc;
  1757. {
  1758.   int str_size;
  1759.  
  1760.   if (!entry->header_read_flag)
  1761.     read_header (desc, entry);
  1762.  
  1763.   entry->symbols = (struct nlist *) xmalloc (entry->header.a_syms);
  1764.  
  1765.   lseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0);
  1766.   if (entry->header.a_syms != read (desc, entry->symbols, entry->header.a_syms))
  1767.     fatal_with_file ("premature end of file in symbols of ", entry);
  1768. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  1769.   fix_symbol_byte_order(entry->symbols, entry->header.a_syms);
  1770. #endif
  1771.   lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  1772.   if (sizeof str_size != read (desc, &str_size, sizeof str_size))
  1773.     fatal_with_file ("bad string table size in ", entry);
  1774. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  1775.   fix_byte_order(&str_size, sizeof(str_size));
  1776. #endif
  1777.   entry->string_size = str_size;
  1778. }
  1779.  
  1780. /* Read the string table of file ENTRY into core.
  1781.    Assume it is already open, on descriptor DESC.
  1782.    Also record whether a GDB symbol segment follows the string table.  */
  1783.  
  1784. void
  1785. read_entry_strings (desc, entry)
  1786.      struct file_entry *entry;
  1787.      int desc;
  1788. {
  1789.   int buffer;
  1790.  
  1791.   if (!entry->header_read_flag)
  1792.     read_header (desc, entry);
  1793.  
  1794.   lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  1795.   if (entry->string_size != read (desc, entry->strings, entry->string_size))
  1796.     fatal_with_file ("premature end of file in strings of ", entry);
  1797.  
  1798.   /* While we are here, see if the file has a symbol segment at the end.
  1799.      For a separate file, just try reading some more.
  1800.      For a library member, compare current pos against total size.  */
  1801.   if (entry->superfile)
  1802.     {
  1803.       if (entry->total_size == N_STROFF (entry->header) + entry->string_size)
  1804.     return;
  1805.     }
  1806.   else
  1807.     {
  1808.       buffer = read (desc, &buffer, sizeof buffer);
  1809.       if (buffer == 0)
  1810.     return;
  1811.       if (buffer != sizeof buffer)
  1812.     fatal_with_file ("premature end of file in GDB symbol segment of ", entry);
  1813.     }
  1814.  
  1815.   entry->symseg_offset = N_STROFF (entry->header) + entry->string_size;
  1816. }
  1817.  
  1818. /* Read in the symbols of all input files.  */
  1819.  
  1820. void read_file_symbols (), read_entry_symbols (), read_entry_strings ();
  1821. void enter_file_symbols (), enter_global_ref (), search_library ();
  1822.  
  1823. void
  1824. load_symbols ()
  1825. {
  1826.   register int i;
  1827.  
  1828.   if (trace_files) fprintf (stderr, "Loading symbols:\n\n");
  1829.  
  1830.   for (i = 0; i < number_of_files; i++)
  1831.     {
  1832.       register struct file_entry *entry = &file_table[i];
  1833.       read_file_symbols (entry);
  1834.     }
  1835.  
  1836.   if (trace_files) fprintf (stderr, "\n");
  1837. }
  1838.  
  1839. /* If ENTRY is a rel file, read its symbol and string sections into core.
  1840.    If it is a library, search it and load the appropriate members
  1841.    (which means calling this function recursively on those members).  */
  1842.  
  1843. void
  1844. read_file_symbols (entry)
  1845.      register struct file_entry *entry;
  1846. {
  1847.   register int desc;
  1848.   register int len;
  1849.   struct exec hdr;
  1850.  
  1851.   desc = file_open (entry);
  1852.  
  1853.   len = read (desc, &hdr, sizeof hdr);
  1854.   if (len != sizeof hdr)
  1855.     fatal_with_file ("failure reading header of ", entry);
  1856. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  1857.   fix_exec_header_byte_order(&hdr);
  1858. #endif
  1859.   if (!N_BADMAG (hdr))
  1860.     {
  1861.       read_entry_symbols (desc, entry);
  1862.       entry->strings = (char *) alloca (entry->string_size);
  1863.       read_entry_strings (desc, entry);
  1864.       enter_file_symbols (entry);
  1865.       entry->strings = 0;
  1866.     }
  1867.   else
  1868.     {
  1869.       char armag[SARMAG];
  1870.  
  1871.       lseek (desc, 0, 0);
  1872.       if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
  1873.     fatal_with_file ("malformed input file (not rel or archive) ", entry);
  1874.       entry->library_flag = 1;
  1875.       search_library (desc, entry);
  1876.     }
  1877.  
  1878.   file_close ();
  1879. }
  1880.  
  1881. /* Enter the external symbol defs and refs of ENTRY in the hash table.  */
  1882.  
  1883. void
  1884. enter_file_symbols (entry)
  1885.      struct file_entry *entry;
  1886. {
  1887.   register struct nlist
  1888.     *p,
  1889.     *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1890.   int lowest_set_vector = -1;
  1891.  
  1892.   if (trace_files) prline_file_name (entry, stderr);
  1893.  
  1894.   for (p = entry->symbols; p < end; p++)
  1895.     {
  1896.       if (p->n_type == (N_SETV | N_EXT)) continue;
  1897.       if (set_element_prefixes
  1898.       && set_element_prefixed_p (p->n_un.n_strx + entry->strings))
  1899.     p->n_type += (N_SETA - N_ABS);
  1900.       if (SET_ELEMENT_P (p->n_type))
  1901.     {
  1902.       set_symbol_count++;
  1903.       if (!relocatable_output)
  1904.         enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1905.     }
  1906.       else if (p->n_type == N_WARNING)
  1907.     {
  1908.       char *name = p->n_un.n_strx + entry->strings;
  1909.  
  1910.       /* Grab the next entry.  */
  1911.       p++;
  1912.       if (p->n_type != (N_UNDF | N_EXT))
  1913.         {
  1914.           fprintf (stderr, "%s: Warning symbol found in %s without external reference following.\n",
  1915.                progname, entry->filename);
  1916.           make_executable = 0;
  1917.           p--;        /* Process normally.  */
  1918.         }
  1919.       else
  1920.         {
  1921.           symbol *sp;
  1922.           char *sname = p->n_un.n_strx + entry->strings; 
  1923.           /* Deal with the warning symbol.  */
  1924.           enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1925.           sp = getsym (sname);
  1926.           sp->warning = (char *) xmalloc (strlen(name) + 1);
  1927.           strcpy (sp->warning, name);
  1928.           warning_count++;
  1929.         }
  1930.     }
  1931.       else if (p->n_type & N_EXT)
  1932.     enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1933.       else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  1934.     {
  1935.       if ((p->n_un.n_strx + entry->strings)[0] != LPREFIX)
  1936.         non_L_local_sym_count++;
  1937.       local_sym_count++;
  1938.     }
  1939.       else debugger_sym_count++;
  1940.     }
  1941.  
  1942.    /* Count one for the local symbol that we generate,
  1943.       whose name is the file's name (usually) and whose address
  1944.       is the start of the file's text.  */
  1945.  
  1946.   local_sym_count++;
  1947.   non_L_local_sym_count++;
  1948. }
  1949.  
  1950. /* Enter one global symbol in the hash table.
  1951.    NLIST_P points to the `struct nlist' read from the file
  1952.    that describes the global symbol.  NAME is the symbol's name.
  1953.    ENTRY is the file entry for the file the symbol comes from.
  1954.  
  1955.    The `struct nlist' is modified by placing it on a chain of
  1956.    all such structs that refer to the same global symbol.
  1957.    This chain starts in the `refs' field of the symbol table entry
  1958.    and is chained through the `n_name'.  */
  1959. void
  1960. enter_global_ref (nlist_p, name, entry)
  1961.      register struct nlist *nlist_p;
  1962.      char *name;
  1963.      struct file_entry *entry;
  1964. {
  1965.   register symbol *sp = getsym (name);
  1966.   register int type = nlist_p->n_type;
  1967.   int oldref = sp->referenced;
  1968.   int olddef = sp->defined;
  1969.  
  1970.   nlist_p->n_un.n_name = (char *) sp->refs;
  1971.   sp->refs = nlist_p;
  1972.  
  1973.   sp->referenced = 1;
  1974.   if (type != (N_UNDF | N_EXT) || nlist_p->n_value)
  1975.     {
  1976.       if (!sp->defined || sp->defined == (N_UNDF | N_EXT))
  1977.     sp->defined = type;
  1978.  
  1979.       if (oldref && !olddef)
  1980.     /* It used to be undefined and we're defining it.  */
  1981.     undefined_global_sym_count--;
  1982.  
  1983.       if (!olddef && type == (N_UNDF | N_EXT) && nlist_p->n_value)
  1984.     {
  1985.       /* First definition and it's common.  */
  1986.       common_defined_global_count++;
  1987.       sp->max_common_size = nlist_p->n_value;
  1988.     }
  1989.       else if (olddef && sp->max_common_size && type != (N_UNDF | N_EXT))
  1990.     {
  1991.       /* It used to be common and we're defining it as
  1992.          something else.  */
  1993.       common_defined_global_count--;
  1994.       sp->max_common_size = 0;
  1995.     }
  1996.       else if (olddef && sp->max_common_size && type == (N_UNDF | N_EXT)
  1997.       && sp->max_common_size < nlist_p->n_value)
  1998.     /* It used to be common and this is a new common entry to
  1999.        which we need to pay attention.  */
  2000.     sp->max_common_size = nlist_p->n_value;
  2001.  
  2002.       /* Are we defining it as a set element?  */
  2003.       if (SET_ELEMENT_P (type)
  2004.       && (!olddef || (olddef && sp->max_common_size)))
  2005.     set_vector_count++;
  2006.       /* As an indirection?  */
  2007.       else if (type == (N_INDR | N_EXT))
  2008.     {
  2009.       /* Indirect symbols value should be modified to point
  2010.          a symbol being equivalenced to. */
  2011.       nlist_p->n_value
  2012.         = (unsigned int) getsym ((nlist_p + 1)->n_un.n_strx
  2013.                      + entry->strings);
  2014.       if ((symbol *) nlist_p->n_value == sp)
  2015.         {
  2016.           /* Somebody redefined a symbol to be itself.  */
  2017.           fprintf (stderr, "%s: Symbol %s indirected to itself.\n",
  2018.                entry->filename, name);
  2019.           /* Rewrite this symbol as being a global text symbol
  2020.          with value 0.  */
  2021.           nlist_p->n_type = sp->defined = N_TEXT | N_EXT;
  2022.           nlist_p->n_value = 0;
  2023.           /* Don't make the output executable.  */
  2024.           make_executable = 0;
  2025.         }
  2026.       else
  2027.         global_indirect_count++;
  2028.     }
  2029.     }
  2030.   else
  2031.     if (!oldref)
  2032. #ifndef DOLLAR_KLUDGE
  2033.       undefined_global_sym_count++;
  2034. #else
  2035.       {
  2036.     if (entry->superfile && type == (N_UNDF | N_EXT) && name[1] == '$')
  2037.       {
  2038.         /* This is an (ISI?) $-conditional; skip it */
  2039.         sp->referenced = 0;
  2040.         if (sp->trace)
  2041.           {
  2042.         fprintf (stderr, "symbol %s is a $-conditional ignored in ", sp->name);
  2043.         print_file_name (entry, stderr);
  2044.         fprintf (stderr, "\n");
  2045.           }
  2046.         return;
  2047.       }
  2048.     else
  2049.       undefined_global_sym_count++;
  2050.       }
  2051. #endif
  2052.  
  2053.   if (sp == end_symbol && entry->just_syms_flag && !T_flag_specified)
  2054.     text_start = nlist_p->n_value;
  2055.  
  2056.   if (sp->trace)
  2057.     {
  2058.       register char *reftype;
  2059.       switch (type & N_TYPE)
  2060.     {
  2061.     case N_UNDF:
  2062.       if (nlist_p->n_value)
  2063.         reftype = "defined as common";
  2064.       else reftype = "referenced";
  2065.       break;
  2066.  
  2067.     case N_ABS:
  2068.       reftype = "defined as absolute";
  2069.       break;
  2070.  
  2071.     case N_TEXT:
  2072.       reftype = "defined in text section";
  2073.       break;
  2074.  
  2075.     case N_DATA:
  2076.       reftype = "defined in data section";
  2077.       break;
  2078.  
  2079.     case N_BSS:
  2080.       reftype = "defined in BSS section";
  2081.       break;
  2082.  
  2083.     case N_SETT:
  2084.       reftype = "is a text set element";
  2085.       break;
  2086.  
  2087.     case N_SETD:
  2088.       reftype = "is a data set element";
  2089.       break;
  2090.  
  2091.     case N_SETB:
  2092.       reftype = "is a BSS set element";
  2093.       break;
  2094.  
  2095.     case N_SETA:
  2096.       reftype = "is an absolute set element";
  2097.       break;
  2098.  
  2099.     case N_SETV:
  2100.       reftype = "defined in data section as vector";
  2101.       break;
  2102.  
  2103.     case N_INDR:
  2104.       reftype = (char *) alloca (23
  2105.                      + strlen ((nlist_p + 1)->n_un.n_strx
  2106.                            + entry->strings));
  2107.       sprintf (reftype, "defined equivalent to %s",
  2108.            (nlist_p + 1)->n_un.n_strx + entry->strings);
  2109.       break;
  2110.  
  2111. #if TARGET_MACHINE==TARGET_SEQUENT
  2112.     case N_SHUNDF:
  2113.       reftype = "shared undf";
  2114.       break;
  2115.  
  2116. /* These conflict with cases above.
  2117.     case N_SHDATA:
  2118.       reftype = "shared data";
  2119.       break;
  2120.  
  2121.     case N_SHBSS:
  2122.       reftype = "shared BSS";
  2123.       break;
  2124. */
  2125.     default:
  2126.       reftype = "I don't know this type";
  2127.       break;
  2128. #endif
  2129.     }
  2130.  
  2131.       fprintf (stderr, "symbol %s %s in ", sp->name, reftype);
  2132.       print_file_name (entry, stderr);
  2133.       fprintf (stderr, "\n");
  2134.     }
  2135. }
  2136.  
  2137. /* This return 0 if the given file entry's symbol table does *not*
  2138.    contain the nlist point entry, and it returns the files entry
  2139.    pointer (cast to unsigned long) if it does. */
  2140.  
  2141. #if 0
  2142. unsigned long
  2143. contains_symbol (entry, n_ptr)
  2144.      struct file_entry *entry;
  2145.      register struct nlist *n_ptr;
  2146. {
  2147.   if (n_ptr >= entry->symbols &&
  2148.       n_ptr < (entry->symbols
  2149.            + (entry->header.a_syms / sizeof (struct nlist))))
  2150.     return (unsigned long) entry;
  2151.   return 0;
  2152. }
  2153. #endif
  2154.  
  2155.  
  2156. /* Searching libraries */
  2157.  
  2158. struct file_entry *decode_library_subfile ();
  2159. void linear_library (), symdef_library ();
  2160.  
  2161. /* Search the library ENTRY, already open on descriptor DESC.
  2162.    This means deciding which library members to load,
  2163.    making a chain of `struct file_entry' for those members,
  2164.    and entering their global symbols in the hash table.  */
  2165.  
  2166. void
  2167. search_library (desc, entry)
  2168.      int desc;
  2169.      struct file_entry *entry;
  2170. {
  2171.   int member_length;
  2172.   register char *name;
  2173.   register struct file_entry *subentry;
  2174.  
  2175.   if (!undefined_global_sym_count) return;
  2176.  
  2177.   /* Examine its first member, which starts SARMAG bytes in.  */
  2178.   subentry = decode_library_subfile (desc, entry, SARMAG, &member_length);
  2179.   if (!subentry) return;
  2180.  
  2181.   name = subentry->filename;
  2182.   free (subentry);
  2183.  
  2184.   /* Search via __.SYMDEF if that exists, else linearly.  */
  2185.  
  2186.   if (!strcmp (name, "__.SYMDEF"))
  2187.     symdef_library (desc, entry, member_length);
  2188.   else
  2189.     linear_library (desc, entry);
  2190. }
  2191.  
  2192. /* Construct and return a file_entry for a library member.
  2193.    The library's file_entry is library_entry, and the library is open on DESC.
  2194.    SUBFILE_OFFSET is the byte index in the library of this member's header.
  2195.    We store the length of the member into *LENGTH_LOC.  */
  2196.  
  2197. struct file_entry *
  2198. decode_library_subfile (desc, library_entry, subfile_offset, length_loc)
  2199.      int desc;
  2200.      struct file_entry *library_entry;
  2201.      int subfile_offset;
  2202.      int *length_loc;
  2203. {
  2204.   int bytes_read;
  2205.   register int namelen;
  2206.   int member_length;
  2207.   register char *name;
  2208.   struct ar_hdr hdr1;
  2209.   register struct file_entry *subentry;
  2210.  
  2211.   lseek (desc, subfile_offset, 0);
  2212.  
  2213.   bytes_read = read (desc, &hdr1, sizeof hdr1);
  2214.   if (!bytes_read)
  2215.     return 0;        /* end of archive */
  2216.  
  2217.   if (sizeof hdr1 != bytes_read)
  2218.     fatal_with_file ("malformed library archive ", library_entry);
  2219.  
  2220.   if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  2221.     fatal_with_file ("malformatted header of archive member in ", library_entry);
  2222.  
  2223.   subentry = (struct file_entry *) xmalloc (sizeof (struct file_entry));
  2224.   bzero (subentry, sizeof (struct file_entry));
  2225.  
  2226.   for (namelen = 0;
  2227.        namelen < sizeof hdr1.ar_name
  2228.        && hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' '
  2229.        && hdr1.ar_name[namelen] != '/';
  2230.        namelen++);
  2231.  
  2232.   name = (char *) xmalloc (namelen+1);
  2233.   strncpy (name, hdr1.ar_name, namelen);
  2234.   name[namelen] = 0;
  2235.  
  2236.   subentry->filename = name;
  2237.   subentry->local_sym_name = name;
  2238.   subentry->symbols = 0;
  2239.   subentry->strings = 0;
  2240.   subentry->subfiles = 0;
  2241.   subentry->starting_offset = subfile_offset + sizeof hdr1;
  2242.   subentry->superfile = library_entry;
  2243.   subentry->library_flag = 0;
  2244.   subentry->header_read_flag = 0;
  2245.   subentry->just_syms_flag = 0;
  2246.   subentry->chain = 0;
  2247.   subentry->total_size = member_length;
  2248.  
  2249.   (*length_loc) = member_length;
  2250.  
  2251.   return subentry;
  2252. }
  2253.  
  2254. int subfile_wanted_p ();
  2255.  
  2256. /* Search a library that has a __.SYMDEF member.
  2257.    DESC is a descriptor on which the library is open.
  2258.      The file pointer is assumed to point at the __.SYMDEF data.
  2259.    ENTRY is the library's file_entry.
  2260.    MEMBER_LENGTH is the length of the __.SYMDEF data.  */
  2261.  
  2262. void
  2263. symdef_library (desc, entry, member_length)
  2264.      int desc;
  2265.      struct file_entry *entry;
  2266.      int member_length;
  2267. {
  2268.   int *symdef_data = (int *) xmalloc (member_length);
  2269.   register struct symdef *symdef_base;
  2270.   char *sym_name_base;
  2271.   int number_of_symdefs;
  2272.   int length_of_strings;
  2273.   int not_finished;
  2274.   int bytes_read;
  2275.   register int i;
  2276.   struct file_entry *prev = 0;
  2277.   int prev_offset = 0;
  2278.  
  2279.   bytes_read = read (desc, symdef_data, member_length);
  2280.   if (bytes_read != member_length)
  2281.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2282. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  2283.   fix_byte_order(symdef_data, sizeof(*symdef_data));
  2284. #endif
  2285.   number_of_symdefs = *symdef_data / sizeof (struct symdef);
  2286.   if (number_of_symdefs < 0 ||
  2287.        number_of_symdefs * sizeof (struct symdef) + 2 * sizeof (int) > member_length)
  2288.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2289.  
  2290.   symdef_base = (struct symdef *) (symdef_data + 1);
  2291. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  2292.   for (i = 0; i < number_of_symdefs; ++i)
  2293.     {
  2294.       fix_byte_order(&symdef_base[i].symbol_name_string_index, sizeof(int));
  2295.       fix_byte_order(&symdef_base[i].library_member_offset, sizeof(int));
  2296.     }
  2297.   fix_byte_order(symdef_base + number_of_symdefs, sizeof(int));
  2298. #endif
  2299.   length_of_strings = *(int *) (symdef_base + number_of_symdefs);
  2300.  
  2301.   if (length_of_strings < 0
  2302.       || number_of_symdefs * sizeof (struct symdef) + length_of_strings
  2303.       + 2 * sizeof (int) != member_length)
  2304.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2305.  
  2306.   sym_name_base = sizeof (int) + (char *) (symdef_base + number_of_symdefs);
  2307.  
  2308.   /* Check all the string indexes for validity.  */
  2309.  
  2310.   for (i = 0; i < number_of_symdefs; i++)
  2311.     {
  2312.       register int index = symdef_base[i].symbol_name_string_index;
  2313.       if (index < 0 || index >= length_of_strings
  2314.       || (index && *(sym_name_base + index - 1)))
  2315.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2316.     }
  2317.  
  2318.   /* Search the symdef data for members to load.
  2319.      Do this until one whole pass finds nothing to load.  */
  2320.  
  2321.   not_finished = 1;
  2322.   while (not_finished)
  2323.     {
  2324.       not_finished = 0;
  2325.  
  2326.       /* Scan all the symbols mentioned in the symdef for ones that we need.
  2327.      Load the library members that contain such symbols.  */
  2328.  
  2329.       for (i = 0;
  2330.        (i < number_of_symdefs
  2331.         && (undefined_global_sym_count || common_defined_global_count));
  2332.        i++)
  2333.     if (symdef_base[i].symbol_name_string_index >= 0)
  2334.       {
  2335.         register symbol *sp;
  2336.  
  2337.         sp = getsym_soft (sym_name_base
  2338.                   + symdef_base[i].symbol_name_string_index);
  2339.  
  2340.         /* If we find a symbol that appears to be needed, think carefully
  2341.            about the archive member that the symbol is in.  */
  2342.  
  2343.         if (sp && ((sp->referenced && !sp->defined)
  2344. #if 1        
  2345.                || (sp->defined && sp->max_common_size))
  2346. #endif
  2347.                )
  2348.           {
  2349.         int junk;
  2350.         register int j;
  2351.         register int offset = symdef_base[i].library_member_offset;
  2352.         struct file_entry *subentry;
  2353.  
  2354.         /* Don't think carefully about any archive member
  2355.            more than once in a given pass.  */
  2356.  
  2357.         if (prev_offset == offset)
  2358.           continue;
  2359.         prev_offset = offset;
  2360.  
  2361.         /* Read the symbol table of the archive member.  */
  2362.  
  2363.         subentry = decode_library_subfile (desc, entry, offset, &junk);
  2364.         if (subentry == 0)
  2365.           fatal ("invalid offset for %s in symbol table of %s",
  2366.              sym_name_base
  2367.              + symdef_base[i].symbol_name_string_index,
  2368.              entry->filename);
  2369.         read_entry_symbols (desc, subentry);
  2370.         subentry->strings = (char *) malloc (subentry->string_size);
  2371.         read_entry_strings (desc, subentry);
  2372.  
  2373.         /* Now scan the symbol table and decide whether to load.  */
  2374.  
  2375.         if (!subfile_wanted_p (subentry))
  2376.           {
  2377.             free (subentry->symbols);
  2378.             free (subentry);
  2379.           }
  2380.         else
  2381.           {
  2382.             /* This member is needed; load it.
  2383.                Since we are loading something on this pass,
  2384.                we must make another pass through the symdef data.  */
  2385.  
  2386.             not_finished = 1;
  2387.  
  2388.             enter_file_symbols (subentry);
  2389.  
  2390.             if (prev)
  2391.               prev->chain = subentry;
  2392.             else entry->subfiles = subentry;
  2393.             prev = subentry;
  2394.  
  2395.             /* Clear out this member's symbols from the symdef data
  2396.                so that following passes won't waste time on them.  */
  2397.  
  2398.             for (j = 0; j < number_of_symdefs; j++)
  2399.               {
  2400.             if (symdef_base[j].library_member_offset == offset)
  2401.               symdef_base[j].symbol_name_string_index = -1;
  2402.               }
  2403.           }
  2404.  
  2405.         /* We'll read the strings again if we need them again.  */
  2406.         free (subentry->strings);
  2407.         subentry->strings = 0;
  2408.           }
  2409.       }
  2410.     }
  2411.  
  2412.   free (symdef_data);
  2413. }
  2414.  
  2415.  
  2416. /* Handle a subentry for a file with no __.SYMDEF. */
  2417.  
  2418. process_subentry (desc, subentry, entry, prev_addr)
  2419.      int desc;
  2420.      register struct file_entry *subentry;
  2421.      struct file_entry **prev_addr, *entry;
  2422. {
  2423.   register struct file_entry *prev = *prev_addr;
  2424.  
  2425.   read_entry_symbols (desc, subentry);
  2426.   subentry->strings = (char *) alloca (subentry->string_size);
  2427.   read_entry_strings (desc, subentry);
  2428.  
  2429.   if (!subfile_wanted_p (subentry))
  2430.     {
  2431.       free (subentry->symbols);
  2432.       free (subentry);
  2433.     }
  2434.   else
  2435.     {
  2436.       enter_file_symbols (subentry);
  2437.  
  2438.       if (prev)
  2439.     prev->chain = subentry;
  2440.       else
  2441.     entry->subfiles = subentry;
  2442.       prev = subentry;
  2443.       subentry->strings = 0; /* Since space will dissapear on return */
  2444.     }
  2445. }
  2446.  
  2447. /* Search a library that has no __.SYMDEF.
  2448.    ENTRY is the library's file_entry.
  2449.    DESC is the descriptor it is open on.  */
  2450.  
  2451. void
  2452. linear_library (desc, entry)
  2453.      int desc;
  2454.      struct file_entry *entry;
  2455. {
  2456.   struct file_entry *prev = 0;
  2457.   register int this_subfile_offset = SARMAG;
  2458.  
  2459.   while (undefined_global_sym_count || common_defined_global_count)
  2460.     {
  2461.       int member_length;
  2462.       register struct file_entry *subentry;
  2463.  
  2464.       subentry = decode_library_subfile (desc, entry, this_subfile_offset,
  2465.                      &member_length);
  2466.       if (!subentry) return;
  2467.  
  2468.       process_subentry (desc, subentry, entry, &prev);
  2469.       this_subfile_offset += member_length + sizeof (struct ar_hdr);
  2470.       if (this_subfile_offset & 1) this_subfile_offset++;
  2471.     }
  2472. }
  2473.  
  2474. /* ENTRY is an entry for a library member.
  2475.    Its symbols have been read into core, but not entered.
  2476.    Return nonzero if we ought to load this member.  */
  2477. int
  2478. subfile_wanted_p (entry)
  2479.      struct file_entry *entry;
  2480. {
  2481.   register struct nlist *p;
  2482.   register struct nlist *end
  2483.     = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2484. #ifdef DOLLAR_KLUDGE
  2485.   register int dollar_cond = 0;
  2486. #endif
  2487.  
  2488.   for (p = entry->symbols; p < end; p++)
  2489.     {
  2490.       register int type = p->n_type;
  2491.       register char *name = p->n_un.n_strx + entry->strings;
  2492.  
  2493.       /* If the symbol has an interesting definition, we could
  2494.      potentially want it.  */
  2495.       if (type & N_EXT
  2496.       && (type != (N_UNDF | N_EXT) || p->n_value
  2497.  
  2498. #ifdef DOLLAR_KLUDGE
  2499.            || name[1] == '$'
  2500. #endif
  2501.           )
  2502. #ifdef foobar          
  2503.       && !SET_ELEMENT_P (type)
  2504.       && !set_element_prefixed_p (name)
  2505. #endif          
  2506.           )
  2507.     {
  2508.       register symbol *sp = getsym_soft (name);
  2509.  
  2510. #ifdef DOLLAR_KLUDGE
  2511.       if (name[1] == '$')
  2512.         {
  2513.           sp = getsym_soft (&name[2]);
  2514.           dollar_cond = 1;
  2515.           if (!sp) continue;
  2516.           if (sp->referenced)
  2517.         {
  2518.           if (write_map)
  2519.             {
  2520.               print_file_name (entry, stdout);
  2521.               fprintf (stdout, " needed due to $-conditional %s\n", name);
  2522.             }
  2523.           return 1;
  2524.         }
  2525.           continue;
  2526.         }
  2527. #endif
  2528.  
  2529.       /* If this symbol has not been hashed, we can't be looking for it. */
  2530.  
  2531.       if (!sp) continue;
  2532.  
  2533.       if ((sp->referenced && !sp->defined)
  2534. #ifdef foobar          
  2535.           || (sp->defined && sp->max_common_size)
  2536. #endif
  2537.           )
  2538.         {
  2539.           /* This is a symbol we are looking for.  It is either
  2540.              not yet defined or defined as a common.  */
  2541. #ifdef DOLLAR_KLUDGE
  2542.           if (dollar_cond) continue;
  2543. #endif
  2544.           if (type == (N_UNDF | N_EXT))
  2545.         {
  2546.           /* Symbol being defined as common.
  2547.              Remember this, but don't load subfile just for this.  */
  2548. #if foobar
  2549.           /* If it didn't used to be common, up the count of
  2550.              common symbols.  */
  2551.           if (!sp->max_common_size)
  2552.             common_defined_global_count++;
  2553. #endif
  2554.           if (sp->max_common_size < p->n_value)
  2555.             sp->max_common_size = p->n_value;
  2556.           if (!sp->defined)
  2557.             undefined_global_sym_count--;
  2558.           sp->defined = 1;
  2559.           continue;
  2560.         }
  2561.  
  2562.           if (write_map)
  2563.         {
  2564.           print_file_name (entry, stdout);
  2565.           fprintf (stdout, " needed due to %s\n", sp->name);
  2566.         }
  2567.           return 1;
  2568.         }
  2569.     }
  2570.     }
  2571.  
  2572.   return 0;
  2573. }
  2574.  
  2575. void consider_file_section_lengths (), relocate_file_addresses ();
  2576.  
  2577. /* Having entered all the global symbols and found the sizes of sections
  2578.    of all files to be linked, make all appropriate deductions from this data.
  2579.  
  2580.    We propagate global symbol values from definitions to references.
  2581.    We compute the layout of the output file and where each input file's
  2582.    contents fit into it.  */
  2583.  
  2584. void
  2585. digest_symbols ()
  2586. {
  2587.   register int i;
  2588.   int setv_fill_count = 0;
  2589.  
  2590.   if (trace_files)
  2591.     fprintf (stderr, "Digesting symbol information:\n\n");
  2592.  
  2593.   /* Compute total size of sections */
  2594.  
  2595.   each_file (consider_file_section_lengths, 0);
  2596.  
  2597.   /* If necessary, pad text section to full page in the file.
  2598.      Include the padding in the text segment size.  */
  2599.  
  2600. #ifdef NMAGIC
  2601.   if (magic == ZMAGIC || magic == NMAGIC)
  2602. #else
  2603.   if (magic == ZMAGIC)
  2604. #endif
  2605.     {
  2606.       int text_end = text_size + N_TXTOFF (outheader);
  2607.       text_pad = ((text_end + page_size - 1) & (- page_size)) - text_end;
  2608.       text_size += text_pad;
  2609.     }
  2610.  
  2611.   outheader.a_text = text_size;
  2612. #if TARGET_MACHINE==TARGET_SEQUENT
  2613.   outheader.a_text += N_ADDRADJ (outheader);
  2614. #endif
  2615.  
  2616.   /* Make the data segment address start in memory on a suitable boundary.  */
  2617.  
  2618.   if (! Tdata_flag_specified)
  2619.     data_start = N_DATADDR (outheader) + text_start - N_TXTADDR (outheader);
  2620. #ifdef foobar
  2621.   /* Make sure bss starts out aligned as much as anyone can want.  */
  2622.  
  2623.   data_size = (data_size + sizeof(double) - 1) & ~(sizeof(double)-1);
  2624. #endif
  2625.   /* Set up the set element vector */
  2626.  
  2627.   if (!relocatable_output)
  2628.     {
  2629.       /* The set sector size is the number of set elements + a word
  2630.          for each symbol for the length word at the beginning of the
  2631.      vector, plus a word for each symbol for a zero at the end of
  2632.      the vector (for incremental linking).  */
  2633.       set_sect_size
  2634.     = (2 * set_symbol_count + set_vector_count) * sizeof (unsigned long);
  2635.       set_sect_start = data_start + data_size;
  2636.       data_size += set_sect_size;
  2637.       set_vectors = (unsigned long *) xmalloc (set_sect_size);
  2638. #if 1
  2639.       setv_fill_count = 0;
  2640. #endif      
  2641.     }
  2642.  
  2643.   /* Compute start addresses of each file's sections and symbols.  */
  2644.  
  2645.   each_full_file (relocate_file_addresses, 0);
  2646.  
  2647.   /* Now, for each symbol, verify that it is defined globally at most once.
  2648.      Put the global value into the symbol entry.
  2649.      Common symbols are allocated here, in the BSS section.
  2650.      Each defined symbol is given a '->defined' field
  2651.       which is the correct N_ code for its definition,
  2652.       except in the case of common symbols with -r.
  2653.      Then make all the references point at the symbol entry
  2654.      instead of being chained together. */
  2655.  
  2656.   defined_global_sym_count = 0;
  2657.  
  2658.   for (i = 0; i < TABSIZE; i++)
  2659.     {
  2660.       register symbol *sp;
  2661.       for (sp = symtab[i]; sp; sp = sp->link)
  2662.     {
  2663.       /* For each symbol */
  2664.       register struct nlist *p, *next;
  2665.       int defs = 0, com = sp->max_common_size, erred = 0;
  2666.       struct nlist *first_definition;
  2667.       for (p = sp->refs; p; p = next)
  2668.         {
  2669.           register int type = p->n_type;
  2670.  
  2671.           if (SET_ELEMENT_P (type))
  2672.         {
  2673.           if (relocatable_output)
  2674.             fatal ("internal: global ref to set element with -r");
  2675.           if (!defs++)
  2676.             {
  2677.               sp->value = set_sect_start
  2678.             + setv_fill_count++ * sizeof (unsigned long);
  2679.               sp->defined = N_SETV | N_EXT;
  2680.               first_definition = p;
  2681.             }
  2682.           else if ((sp->defined & ~N_EXT) != N_SETV)
  2683.             {
  2684.               sp->multiply_defined = 1;
  2685.               multiple_def_count++;
  2686.             }
  2687.           set_vectors[setv_fill_count++] = p->n_value;
  2688.         }
  2689.           else if ((type & N_EXT) && type != (N_UNDF | N_EXT))
  2690.         {
  2691.           /* non-common definition */
  2692.           if (defs++ && sp->value != p->n_value)
  2693.             {
  2694.               sp->multiply_defined = 1;
  2695.               multiple_def_count++;
  2696.             }
  2697.           sp->value = p->n_value;
  2698.           sp->defined = type;
  2699.           first_definition = p;
  2700.         }
  2701.           next = (struct nlist *) p->n_un.n_name;
  2702.           p->n_un.n_name = (char *) sp;
  2703.         }
  2704.       /* Allocate as common if defined as common and not defined for real */
  2705.       if (com && !defs)
  2706.         {
  2707.           if (!relocatable_output || force_common_definition)
  2708.         {
  2709.           int align = sizeof (int);
  2710.  
  2711.           /* Round up to nearest sizeof (int).  I don't know
  2712.              whether this is necessary or not (given that
  2713.              alignment is taken care of later), but it's
  2714.              traditional, so I'll leave it in.  Note that if
  2715.              this size alignment is ever removed, ALIGN above
  2716.              will have to be initialized to 1 instead of
  2717.              sizeof (int).  */
  2718.           
  2719.           com = (com + sizeof (int) - 1) & (- sizeof (int));
  2720.  
  2721.           while (!(com & align))
  2722.             align <<= 1;
  2723.  
  2724.           align = align > MAX_ALIGNMENT ? MAX_ALIGNMENT : align;
  2725.  
  2726.           bss_size = ((((bss_size + data_size + data_start)
  2727.                   + (align - 1)) & (- align))
  2728.                   - data_size - data_start);
  2729.  
  2730.           sp->value = data_start + data_size + bss_size;
  2731.           sp->defined = N_BSS | N_EXT;
  2732.           bss_size += com;
  2733.           if (write_map)
  2734.             printf ("Allocating common %s: %x at %x\n",
  2735.                 sp->name, com, sp->value);
  2736.         }
  2737.           else
  2738.         {
  2739.           sp->defined = 0;
  2740.           undefined_global_sym_count++;
  2741.         }
  2742.         }
  2743.       /* Set length word at front of vector and zero byte at end.
  2744.          Reverse the vector itself to put it in file order.  */
  2745.       if ((sp->defined & ~N_EXT) == N_SETV)
  2746.         {
  2747.           unsigned long length_word_index
  2748.         = (sp->value - set_sect_start) / sizeof (unsigned long);
  2749.           unsigned long i, tmp;
  2750.  
  2751.           set_vectors[length_word_index]
  2752.         = setv_fill_count - 1 - length_word_index;
  2753. #ifdef foobar
  2754.           /* Reverse the vector.  */
  2755.           for (i = 1;
  2756.            i < (setv_fill_count - length_word_index - 1) / 2 + 1;
  2757.            i++)
  2758.         {
  2759.           tmp = set_vectors[length_word_index + i];
  2760.           set_vectors[length_word_index + i]
  2761.             = set_vectors[setv_fill_count - i];
  2762.           set_vectors[setv_fill_count - i] = tmp;
  2763. #endif        }
  2764.  
  2765.           set_vectors[setv_fill_count++] = 0;
  2766.         }
  2767.       if (sp->defined)
  2768.         defined_global_sym_count++;
  2769.     }
  2770.     }
  2771.  
  2772. #ifdef foobar
  2773.   /* Make sure end of bss is aligned as much as anyone can want.  */
  2774.  
  2775.   bss_size = (bss_size + sizeof(double) - 1) & ~(sizeof(double)-1);
  2776. #endif
  2777.  
  2778.   if (end_symbol)        /* These are null if -r.  */
  2779.     {
  2780.       etext_symbol->value = text_size + text_start;
  2781.       edata_symbol->value = data_start + data_size;
  2782.       end_symbol->value = data_start + data_size + bss_size;
  2783.     }
  2784.  
  2785.   /* Figure the data_pad now, so that it overlaps with the bss addresses.  */
  2786.  
  2787.   if (specified_data_size && specified_data_size > data_size)
  2788.     data_pad = specified_data_size - data_size;
  2789.  
  2790.   if (magic == ZMAGIC)
  2791.     data_pad = ((data_pad + data_size + page_size - 1) & (- page_size))
  2792.                - data_size;
  2793.  
  2794.   bss_size -= data_pad;
  2795.   if (bss_size < 0) bss_size = 0;
  2796.  
  2797.   data_size += data_pad;
  2798. }
  2799.  
  2800. /* Accumulate the section sizes of input file ENTRY
  2801.    into the section sizes of the output file.  */
  2802.  
  2803. void
  2804. consider_file_section_lengths (entry)
  2805.      register struct file_entry *entry;
  2806. {
  2807.   if (entry->just_syms_flag)
  2808.     return;
  2809.  
  2810.   entry->text_start_address = text_size;
  2811.   /* If there were any vectors, we need to chop them off */
  2812.   text_size += entry->header.a_text;
  2813.   entry->data_start_address = data_size;
  2814.   data_size += entry->header.a_data;
  2815.   entry->bss_start_address = bss_size;
  2816.   bss_size += entry->header.a_bss;
  2817.  
  2818.   text_reloc_size += entry->header.a_trsize;
  2819.   data_reloc_size += entry->header.a_drsize;
  2820. }
  2821.  
  2822. /* Determine where the sections of ENTRY go into the output file,
  2823.    whose total section sizes are already known.
  2824.    Also relocate the addresses of the file's local and debugger symbols.  */
  2825.  
  2826. void
  2827. relocate_file_addresses (entry)
  2828.      register struct file_entry *entry;
  2829. {
  2830.   entry->text_start_address += text_start;
  2831.   /* Note that `data_start' and `data_size' have not yet been
  2832.      adjusted for `data_pad'.  If they had been, we would get the wrong
  2833.      results here.  */
  2834.   entry->data_start_address += data_start;
  2835.   entry->bss_start_address += data_start + data_size;
  2836.  
  2837.   {
  2838.     register struct nlist *p;
  2839.     register struct nlist *end
  2840.       = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2841.  
  2842.     for (p = entry->symbols; p < end; p++)
  2843.       {
  2844.     /* If this belongs to a section, update it by the section's start address */
  2845.     register int type = p->n_type & N_TYPE;
  2846.  
  2847.     switch (type)
  2848.       {
  2849.       case N_TEXT:
  2850.       case N_SETT:
  2851.         p->n_value += entry->text_start_address;
  2852.         break;
  2853.       case N_DATA:
  2854.       case N_SETV:
  2855.       case N_SETD:
  2856.         /* A symbol whose value is in the data section
  2857.            is present in the input file as if the data section
  2858.            started at an address equal to the length of the file's text.  */
  2859.         p->n_value += entry->data_start_address - entry->header.a_text;
  2860.         break;
  2861.       case N_BSS:
  2862.       case N_SETB:
  2863.         /* likewise for symbols with value in BSS.  */
  2864.         p->n_value += entry->bss_start_address
  2865.           - entry->header.a_text - entry->header.a_data;
  2866.         break;
  2867.       }
  2868.       }
  2869.   }
  2870. }
  2871.  
  2872. void describe_file_sections (), list_file_locals ();
  2873.  
  2874. /* Print a complete or partial map of the output file.  */
  2875.  
  2876. void
  2877. print_symbols (outfile)
  2878.      FILE *outfile;
  2879. {
  2880.   register int i;
  2881.  
  2882.   fprintf (outfile, "\nFiles:\n\n");
  2883.  
  2884.   each_file (describe_file_sections, outfile);
  2885.  
  2886.   fprintf (outfile, "\nGlobal symbols:\n\n");
  2887.  
  2888.   for (i = 0; i < TABSIZE; i++)
  2889.     {
  2890.       register symbol *sp;
  2891.       for (sp = symtab[i]; sp; sp = sp->link)
  2892.     {
  2893.       if (sp->defined == 1)
  2894.         fprintf (outfile, "  %s: common, length 0x%x\n", sp->name, sp->max_common_size);
  2895.       if (sp->defined)
  2896.         fprintf (outfile, "  %s: 0x%x\n", sp->name, sp->value);
  2897.       else if (sp->referenced)
  2898.         fprintf (outfile, "  %s: undefined\n", sp->name);
  2899.     }
  2900.     }
  2901.  
  2902.   each_file (list_file_locals, outfile);
  2903. }
  2904.  
  2905. void
  2906. describe_file_sections (entry, outfile)
  2907.      struct file_entry *entry;
  2908.      FILE *outfile;
  2909. {
  2910.   fprintf (outfile, "  ");
  2911.   print_file_name (entry, outfile);
  2912.   if (entry->just_syms_flag)
  2913.     fprintf (outfile, " symbols only\n", 0);
  2914.   else
  2915.     fprintf (outfile, " text %x(%x), data %x(%x), bss %x(%x) hex\n",
  2916.          entry->text_start_address, entry->header.a_text,
  2917.          entry->data_start_address, entry->header.a_data,
  2918.          entry->bss_start_address, entry->header.a_bss);
  2919. }
  2920.  
  2921. void
  2922. list_file_locals (entry, outfile)
  2923.      struct file_entry *entry;
  2924.      FILE *outfile;
  2925. {
  2926.   register struct nlist
  2927.     *p,
  2928.     *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2929.  
  2930.   entry->strings = (char *) alloca (entry->string_size);
  2931.   read_entry_strings (file_open (entry), entry);
  2932.  
  2933.   fprintf (outfile, "\nLocal symbols of ");
  2934.   print_file_name (entry, outfile);
  2935.   fprintf (outfile, ":\n\n");
  2936.  
  2937.   for (p = entry->symbols; p < end; p++)
  2938.     /* If this is a definition,
  2939.        update it if necessary by this file's start address.  */
  2940.     if (!(p->n_type & (N_STAB | N_EXT)))
  2941.       fprintf (outfile, "  %s: 0x%x\n",
  2942.            entry->strings + p->n_un.n_strx, p->n_value);
  2943.  
  2944.   entry->strings = 0;        /* All done with them.  */
  2945. }
  2946.  
  2947.  
  2948. /* Static vars for do_warnings and subroutines of it */
  2949. int list_unresolved_refs;    /* List unresolved refs */
  2950. int list_warning_symbols;    /* List warning syms */
  2951. int list_multiple_defs;        /* List multiple definitions */
  2952.  
  2953. /*
  2954.  * Structure for communication between do_file_warnings and it's
  2955.  * helper routines.  Will in practice be an array of three of these:
  2956.  * 0) Current line, 1) Next line, 2) Source file info.
  2957.  */
  2958. struct line_debug_entry
  2959. {
  2960.   int line;
  2961.   char *filename;
  2962.   struct nlist *Sym;
  2963. };
  2964.  
  2965. void qsort ();
  2966. /*
  2967.  * Helper routines for do_file_warnings.
  2968.  */
  2969.  
  2970. /* Return an integer less than, equal to, or greater than 0 as per the
  2971.    relation between the two relocation entries.  Used by qsort.  */
  2972.  
  2973. int
  2974. relocation_entries_relation (rel1, rel2)
  2975.      struct relocation_info *rel1, *rel2;
  2976. {
  2977.   return RELOC_ADDRESS(rel1) - RELOC_ADDRESS(rel2);
  2978. }
  2979.  
  2980. /* Moves to the next debugging symbol in the file.  USE_DATA_SYMBOLS
  2981.    determines the type of the debugging symbol to look for (DSLINE or
  2982.    SLINE).  STATE_POINTER keeps track of the old and new locatiosn in
  2983.    the file.  It assumes that state_pointer[1] is valid; ie
  2984.    that it.sym points into some entry in the symbol table.  If
  2985.    state_pointer[1].sym == 0, this routine should not be called.  */
  2986.  
  2987. int
  2988. next_debug_entry (use_data_symbols, state_pointer)
  2989.      register int use_data_symbols;
  2990.      /* Next must be passed by reference! */
  2991.      struct line_debug_entry state_pointer[3];
  2992. {
  2993.   register struct line_debug_entry
  2994.     *current = state_pointer,
  2995.     *next = state_pointer + 1,
  2996.     /* Used to store source file */
  2997.     *source = state_pointer + 2;
  2998.   struct file_entry *entry = (struct file_entry *) source->Sym;
  2999.  
  3000.   current->Sym = next->Sym;
  3001.   current->line = next->line;
  3002.   current->filename = next->filename;
  3003.  
  3004.   while (++(next->Sym) < (entry->symbols
  3005.               + entry->header.a_syms/sizeof (struct nlist)))
  3006.     {
  3007.       /* n_type is a char, and N_SOL, N_EINCL and N_BINCL are > 0x80, so
  3008.        * may look negative...therefore, must mask to low bits
  3009.        */
  3010.       switch (next->Sym->n_type & 0xff) 
  3011.     {
  3012.     case N_SLINE:
  3013.       if (use_data_symbols) continue;
  3014.       next->line = next->Sym->n_desc;
  3015.       return 1;
  3016.     case N_DSLINE:
  3017.       if (!use_data_symbols) continue;
  3018.       next->line = next->Sym->n_desc;
  3019.       return 1;
  3020. #ifdef HAVE_SUN_STABS
  3021.     case N_EINCL:
  3022.       next->filename = source->filename;
  3023.       continue;
  3024. #endif
  3025.     case N_SO:
  3026.       source->filename = next->Sym->n_un.n_strx + entry->strings;
  3027.       source->line++;
  3028. #ifdef HAVE_SUN_STABS
  3029.     case N_BINCL:
  3030. #endif
  3031.     case N_SOL:
  3032.       next->filename
  3033.         = next->Sym->n_un.n_strx + entry->strings;
  3034.     default:
  3035.       continue;
  3036.     }
  3037.     }
  3038.   next->Sym = (struct nlist *) 0;
  3039.   return 0;
  3040. }
  3041.  
  3042. /* Create a structure to save the state of a scan through the debug
  3043.    symbols.  USE_DATA_SYMBOLS is set if we should be scanning for
  3044.    DSLINE's instead of SLINE's.  entry is the file entry which points
  3045.    at the symbols to use.  */
  3046.  
  3047. struct line_debug_entry *
  3048. init_debug_scan (use_data_symbols, entry)
  3049.      int use_data_symbols;
  3050.      struct file_entry *entry;
  3051. {
  3052.   struct line_debug_entry
  3053.     *state_pointer
  3054.       = (struct line_debug_entry *)
  3055.     xmalloc (3 * sizeof (struct line_debug_entry));
  3056.   register struct line_debug_entry
  3057.     *current = state_pointer,
  3058.     *next = state_pointer + 1,
  3059.     *source = state_pointer + 2; /* Used to store source file */
  3060.  
  3061.   struct nlist *tmp;
  3062.  
  3063.   for (tmp = entry->symbols;
  3064.        tmp < (entry->symbols
  3065.           + entry->header.a_syms/sizeof (struct nlist));
  3066.        tmp++)
  3067.     if (tmp->n_type == (int) N_SO)
  3068.       break;
  3069.  
  3070.   if (tmp >= (entry->symbols
  3071.           + entry->header.a_syms/sizeof (struct nlist)))
  3072.     {
  3073.       /* I believe this translates to "We lose" */
  3074.       current->filename = next->filename = entry->filename;
  3075.       current->line = next->line = -1;
  3076.       current->Sym = next->Sym = (struct nlist *) 0;
  3077.       return state_pointer;
  3078.     }
  3079.  
  3080.   next->line = source->line = 0;
  3081.   next->filename = source->filename
  3082.     = (tmp->n_un.n_strx + entry->strings);
  3083.   source->Sym = (struct nlist *) entry;
  3084.   next->Sym = tmp;
  3085.  
  3086.   next_debug_entry (use_data_symbols, state_pointer); /* To setup next */
  3087.  
  3088.   if (!next->Sym)        /* No line numbers for this section; */
  3089.                 /* setup output results as appropriate */
  3090.     {
  3091.       if (source->line)
  3092.     {
  3093.       current->filename = source->filename = entry->filename;
  3094.       current->line = -1;    /* Don't print lineno */
  3095.     }
  3096.       else
  3097.     {
  3098.       current->filename = source->filename;
  3099.       current->line = 0;
  3100.     }
  3101.       return state_pointer;
  3102.     }
  3103.  
  3104.  
  3105.   next_debug_entry (use_data_symbols, state_pointer); /* To setup current */
  3106.  
  3107.   return state_pointer;
  3108. }
  3109.  
  3110. /* Takes an ADDRESS (in either text or data space) and a STATE_POINTER
  3111.    which describes the current location in the implied scan through
  3112.    the debug symbols within the file which ADDRESS is within, and
  3113.    returns the source line number which corresponds to ADDRESS.  */
  3114.   
  3115. int
  3116. address_to_line (address, state_pointer)
  3117.      unsigned long address;
  3118.      /* Next must be passed by reference! */
  3119.      struct line_debug_entry state_pointer[3];
  3120. {
  3121.   struct line_debug_entry
  3122.     *current = state_pointer,
  3123.     *next = state_pointer + 1;
  3124.   struct line_debug_entry *tmp_pointer;
  3125.  
  3126.   int use_data_symbols;
  3127.  
  3128.   if (next->Sym)
  3129.     use_data_symbols = (next->Sym->n_type & N_TYPE) == N_DATA;
  3130.   else
  3131.     return current->line;
  3132.  
  3133.   /* Go back to the beginning if we've already passed it.  */
  3134.   if (current->Sym->n_value > address)
  3135.     {
  3136.       tmp_pointer = init_debug_scan (use_data_symbols,
  3137.                      (struct file_entry *)
  3138.                      ((state_pointer + 2)->Sym));
  3139.       state_pointer[0] = tmp_pointer[0];
  3140.       state_pointer[1] = tmp_pointer[1];
  3141.       state_pointer[2] = tmp_pointer[2];
  3142.       free (tmp_pointer);
  3143.     }
  3144.  
  3145.   /* If we're still in a bad way, return -1, meaning invalid line.  */
  3146.   if (current->Sym->n_value > address)
  3147.     return -1;
  3148.  
  3149.   while (next->Sym
  3150.      && next->Sym->n_value <= address
  3151.      && next_debug_entry (use_data_symbols, state_pointer))
  3152.     ;
  3153.   return current->line;
  3154. }
  3155.  
  3156.  
  3157. /* Macros for manipulating bitvectors.  */
  3158. #define    BIT_SET_P(bv, index)    ((bv)[(index) >> 3] & 1 << ((index) & 0x7))
  3159. #define    SET_BIT(bv, index)    ((bv)[(index) >> 3] |= 1 << ((index) & 0x7))
  3160.  
  3161. /* This routine will scan through the relocation data of file ENTRY,
  3162.    printing out references to undefined symbols and references to
  3163.    symbols defined in files with N_WARNING symbols.  If DATA_SEGMENT
  3164.    is non-zero, it will scan the data relocation segment (and use
  3165.    N_DSLINE symbols to track line number); otherwise it will scan the
  3166.    text relocation segment.  Warnings will be printed on the output
  3167.    stream OUTFILE.  Eventually, every nlist symbol mapped through will
  3168.    be marked in the NLIST_BITVECTOR, so we don't repeat ourselves when
  3169.    we scan the nlists themselves.  */
  3170.  
  3171. do_relocation_warnings (entry, data_segment, outfile, nlist_bitvector)
  3172.      struct file_entry *entry;
  3173.      int data_segment;
  3174.      FILE *outfile;
  3175.      unsigned char *nlist_bitvector;
  3176. {
  3177.   struct relocation_info
  3178.     *reloc_start = data_segment ? entry->datarel : entry->textrel,
  3179.     *reloc;
  3180.   int reloc_size
  3181.     = ((data_segment ? entry->header.a_drsize : entry->header.a_trsize)
  3182.        / sizeof (struct relocation_info));
  3183.   int start_of_segment
  3184.     = (data_segment ? entry->data_start_address : entry->text_start_address);
  3185.   struct nlist *start_of_syms = entry->symbols;
  3186.   struct line_debug_entry *state_pointer
  3187.     = init_debug_scan (data_segment != 0, entry);
  3188.   register struct line_debug_entry
  3189.     *current = state_pointer,
  3190.     *next = state_pointer + 1,
  3191.     *source = state_pointer + 2;
  3192.   /* Assigned to generally static values; should not be written into.  */
  3193.   char *errfmt;
  3194.   /* Assigned to alloca'd values cand copied into; should be freed
  3195.      when done.  */
  3196.   char *errmsg;
  3197.   int invalidate_line_number;
  3198.  
  3199.   /* We need to sort the relocation info here.  Sheesh, so much effort
  3200.      for one lousy error optimization. */
  3201.  
  3202.   qsort (reloc_start, reloc_size, sizeof (struct relocation_info),
  3203.      relocation_entries_relation);
  3204.  
  3205.   for (reloc = reloc_start;
  3206.        reloc < (reloc_start + reloc_size);
  3207.        reloc++)
  3208.     {
  3209.       register struct nlist *s;
  3210.       register symbol *g;
  3211.  
  3212.       /* If the relocation isn't resolved through a symbol, continue */
  3213.       if (!RELOC_EXTERN_P(reloc))
  3214.     continue;
  3215.  
  3216.       s = &(entry->symbols[RELOC_SYMBOL(reloc)]);
  3217.  
  3218.       /* Local symbols shouldn't ever be used by relocation info, so
  3219.      the next should be safe.
  3220.      This is, of course, wrong.  References to local BSS symbols can be
  3221.      the targets of relocation info, and they can (must) be
  3222.      resolved through symbols.  However, these must be defined properly,
  3223.      (the assembler would have caught it otherwise), so we can
  3224.      ignore these cases.  */
  3225.       if (!(s->n_type & N_EXT))
  3226.     continue;
  3227.  
  3228.       g = (symbol *) s->n_un.n_name;
  3229.       errmsg = 0;
  3230.  
  3231.       if (!g->defined && list_unresolved_refs) /* Reference */
  3232.     {
  3233.       /* Mark as being noted by relocation warning pass.  */
  3234.       SET_BIT (nlist_bitvector, s - start_of_syms);
  3235.       
  3236.       if (g->undef_refs >= MAX_UREFS_PRINTED)    /* Listed too many */
  3237.         continue;
  3238.  
  3239.       /* Undefined symbol which we should mention */
  3240.  
  3241.       if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  3242.         {
  3243.           errfmt = "More undefined symbol %s refs follow";
  3244.           invalidate_line_number = 1;
  3245.         }
  3246.       else
  3247.         {
  3248.           errfmt = "Undefined symbol %s referenced from %s segment";
  3249.           invalidate_line_number = 0;
  3250.         }
  3251.     }
  3252.       else                         /* Defined */
  3253.     {
  3254.       /* Potential symbol warning here */
  3255.       if (!g->warning) continue;
  3256.  
  3257.       /* Mark as being noted by relocation warning pass.  */
  3258.       SET_BIT (nlist_bitvector, s - start_of_syms);
  3259.       
  3260.       errfmt = 0;
  3261.       errmsg = g->warning;
  3262.       invalidate_line_number = 0;
  3263.     }
  3264.       
  3265.  
  3266.       /* If errfmt == 0, errmsg has already been defined.  */
  3267.       if (errfmt != 0)
  3268.     {
  3269.       errmsg = (char *) xmalloc (strlen (errfmt) + strlen (g->name) + 1);
  3270.       sprintf (errmsg, errfmt, g->name, data_segment ? "data" : "text");
  3271.     }
  3272.  
  3273.       address_to_line (RELOC_ADDRESS (reloc) + start_of_segment,
  3274.                state_pointer);
  3275.  
  3276.       if (current->line >=0)
  3277.     fprintf (outfile, "%s:%d: %s\n", current->filename,
  3278.          invalidate_line_number ? 0 : current->line, errmsg);
  3279.       else
  3280.     fprintf (outfile, "%s: %s\n", current->filename, errmsg);
  3281.  
  3282.       if (errfmt != 0)
  3283.     free (errmsg);
  3284.     }
  3285.  
  3286.   free (state_pointer);
  3287. }
  3288.  
  3289. /* Print on OUTFILE a list of all warnings generated by references
  3290.    and/or definitions in the file ENTRY.  List source file and line
  3291.    number if possible, just the .o file if not. */
  3292.  
  3293. void
  3294. do_file_warnings (entry, outfile)
  3295.      struct file_entry *entry;
  3296.      FILE *outfile;
  3297. {
  3298.   int number_of_syms = entry->header.a_syms / sizeof (struct nlist);
  3299.   unsigned char *nlist_bitvector
  3300.     = (unsigned char *) alloca ((number_of_syms >> 3) + 1);
  3301.   struct line_debug_entry *text_scan, *data_scan;
  3302.   int i;
  3303.   char *errfmt, *file_name;
  3304.   int line_number;
  3305.   int dont_allow_symbol_name;
  3306.  
  3307.   bzero (nlist_bitvector, (number_of_syms >> 3) + 1);
  3308.  
  3309.   /* Read in the files strings if they aren't available */
  3310.   if (!entry->strings)
  3311.     {
  3312.       int desc;
  3313.  
  3314.       entry->strings = (char *) alloca (entry->string_size);
  3315.       desc = file_open (entry);
  3316.       read_entry_strings (desc, entry);
  3317.     }
  3318.  
  3319.   read_file_relocation (entry);
  3320.  
  3321.   /* Do text warnings based on a scan through the relocation info.  */
  3322.   do_relocation_warnings (entry, 0, outfile, nlist_bitvector);
  3323.  
  3324.   /* Do data warnings based on a scan through the relocation info.  */
  3325.   do_relocation_warnings (entry, 1, outfile, nlist_bitvector);
  3326.  
  3327.   /* Scan through all of the nlist entries in this file and pick up
  3328.      anything that the scan through the relocation stuff didn't.  */
  3329.  
  3330.   text_scan = init_debug_scan (0, entry);
  3331.   data_scan = init_debug_scan (1, entry);
  3332.  
  3333.   for (i = 0; i < number_of_syms; i++)
  3334.     {
  3335.       struct nlist *s;
  3336.       struct glosym *g;
  3337.  
  3338.       s = entry->symbols + i;
  3339.  
  3340.       if (!(s->n_type & N_EXT))
  3341.     continue;
  3342.  
  3343.       g = (symbol *) s->n_un.n_name;
  3344.       dont_allow_symbol_name = 0;
  3345.  
  3346.       if (list_multiple_defs && g->multiply_defined)
  3347.     {
  3348.       errfmt = "Definition of symbol %s (multiply defined)";
  3349.       switch (s->n_type)
  3350.         {
  3351.         case N_TEXT | N_EXT:
  3352.           line_number = address_to_line (s->n_value, text_scan);
  3353.           file_name = text_scan[0].filename;
  3354.           break;
  3355.         case N_DATA | N_EXT:
  3356.           line_number = address_to_line (s->n_value, data_scan);
  3357.           file_name = data_scan[0].filename;
  3358.           break;
  3359.         case N_SETA | N_EXT:
  3360.         case N_SETT | N_EXT:
  3361.         case N_SETD | N_EXT:
  3362.         case N_SETB | N_EXT:
  3363.           if (g->multiply_defined == 2)
  3364.         continue;
  3365.           errfmt = "First set element definition of symbol %s (multiply defined)";
  3366.           break;
  3367.         default:
  3368.           continue;        /* Don't print out multiple defs
  3369.                    at references.  */
  3370.         }
  3371.     }
  3372.       else if (BIT_SET_P (nlist_bitvector, i))
  3373.     continue;
  3374.       else if (list_unresolved_refs && !g->defined)
  3375.     {
  3376.       if (g->undef_refs >= MAX_UREFS_PRINTED)
  3377.         continue;
  3378.       
  3379.       if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  3380.         errfmt = "More undefined \"%s\" refs follow";
  3381.       else
  3382.         errfmt = "Undefined symbol \"%s\" referenced";
  3383.       line_number = -1;
  3384.     }
  3385.       else if (g->warning)
  3386.     {
  3387.       /* There are two cases in which we don't want to
  3388.          do this.  The first is if this is a definition instead of
  3389.          a reference.  The second is if it's the reference used by
  3390.          the warning stabs itself.  */
  3391.       if (s->n_type != (N_EXT | N_UNDF)
  3392.           || (i && (s-1)->n_type == N_WARNING))
  3393.         continue;
  3394.  
  3395.       errfmt = g->warning;
  3396.       line_number = -1;
  3397.       dont_allow_symbol_name = 1;
  3398.     }
  3399.       else
  3400.     continue;
  3401.       
  3402.       if (line_number == -1)
  3403.     fprintf (outfile, "%s: ", entry->filename);
  3404.       else
  3405.     fprintf (outfile, "%s:%d: ", file_name, line_number);
  3406.  
  3407.       if (dont_allow_symbol_name)
  3408.     fprintf (outfile, "%s", errfmt);
  3409.       else
  3410.     fprintf (outfile, errfmt, g->name);
  3411.  
  3412.       fputc ('\n', outfile);
  3413.     }
  3414.   free (text_scan);
  3415.   free (data_scan);
  3416.   entry->strings = 0;        /* Since it will dissapear anyway.  */
  3417. }
  3418.  
  3419. do_warnings (outfile)
  3420.      FILE *outfile;
  3421. {
  3422.   int i;
  3423.  
  3424.   list_unresolved_refs = !relocatable_output && undefined_global_sym_count;
  3425.   list_warning_symbols = warning_count;
  3426.   list_multiple_defs = multiple_def_count != 0;
  3427.  
  3428.   if (!(list_unresolved_refs ||
  3429.     list_warning_symbols ||
  3430.     list_multiple_defs      ))
  3431.     /* No need to run this routine */
  3432.     return;
  3433.  
  3434.   each_file (do_file_warnings, outfile);
  3435.  
  3436.   if (list_unresolved_refs || list_multiple_defs)
  3437.     make_executable = 0;
  3438. }
  3439.  
  3440. /* Write the output file */
  3441.  
  3442. void
  3443. write_output ()
  3444. {
  3445.   struct stat statbuf;
  3446.   int filemode;
  3447.  
  3448.   outdesc = open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  3449.   if (outdesc < 0) perror_name (output_filename);
  3450.  
  3451.   if (fstat (outdesc, &statbuf) < 0)
  3452.     perror_name (output_filename);
  3453.  
  3454.   filemode = statbuf.st_mode;
  3455.  
  3456.   chmod (output_filename, filemode & ~0111);
  3457.  
  3458.   /* Output the a.out header.  */
  3459.   write_header ();
  3460.  
  3461.   /* Output the text and data segments, relocating as we go.  */
  3462.   write_text ();
  3463.   write_data ();
  3464.  
  3465.   /* Output the merged relocation info, if requested with `-r'.  */
  3466.   if (relocatable_output)
  3467.     write_rel ();
  3468.  
  3469.   /* Output the symbol table (both globals and locals).  */
  3470.   write_syms ();
  3471.  
  3472.   /* Copy any GDB symbol segments from input files.  */
  3473.   write_symsegs ();
  3474.  
  3475.   close (outdesc);
  3476.  
  3477.   if (chmod (output_filename, filemode | 0111) == -1)
  3478.     perror_name (output_filename);
  3479. }
  3480.  
  3481. void modify_location (), perform_relocation (), copy_text (), copy_data ();
  3482.  
  3483. void
  3484. write_header ()
  3485. {
  3486.   N_SET_MAGIC (outheader, magic);
  3487.   outheader.a_text = text_size;
  3488. #if TARGET_MACHINE==TARGET_SEQUENT
  3489.   outheader.a_text += N_ADDRADJ (outheader);
  3490. #endif
  3491.   outheader.a_data = data_size;
  3492.   outheader.a_bss = bss_size;
  3493.   outheader.a_entry = (entry_symbol ? entry_symbol->value
  3494.                : text_start + entry_offset);
  3495. #ifdef COFF_ENCAPSULATE
  3496.   if (need_coff_header)
  3497.     {
  3498.       /* We are encapsulating BSD format within COFF format.  */
  3499.       struct coffscn *tp, *dp, *bp;
  3500.  
  3501.       tp = &coffheader.scns[0];
  3502.       dp = &coffheader.scns[1];
  3503.       bp = &coffheader.scns[2];
  3504.  
  3505.       strcpy (tp->s_name, ".text");
  3506.       tp->s_paddr = text_start;
  3507.       tp->s_vaddr = text_start;
  3508.       tp->s_size = text_size;
  3509.       tp->s_scnptr = sizeof (struct coffheader) + sizeof (struct exec);
  3510.       tp->s_relptr = 0;
  3511.       tp->s_lnnoptr = 0;
  3512.       tp->s_nreloc = 0;
  3513.       tp->s_nlnno = 0;
  3514.       tp->s_flags = 0x20;
  3515.       strcpy (dp->s_name, ".data");
  3516.       dp->s_paddr = data_start;
  3517.       dp->s_vaddr = data_start;
  3518.       dp->s_size = data_size;
  3519.       dp->s_scnptr = tp->s_scnptr + tp->s_size;
  3520.       dp->s_relptr = 0;
  3521.       dp->s_lnnoptr = 0;
  3522.       dp->s_nreloc = 0;
  3523.       dp->s_nlnno = 0;
  3524.       dp->s_flags = 0x40;
  3525.       strcpy (bp->s_name, ".bss");
  3526.       bp->s_paddr = dp->s_vaddr + dp->s_size;
  3527.       bp->s_vaddr = bp->s_paddr;
  3528.       bp->s_size = bss_size;
  3529.       bp->s_scnptr = 0;
  3530.       bp->s_relptr = 0;
  3531.       bp->s_lnnoptr = 0;
  3532.       bp->s_nreloc = 0;
  3533.       bp->s_nlnno = 0;
  3534.       bp->s_flags = 0x80;
  3535.  
  3536.       coffheader.f_magic = COFF_MAGIC;
  3537.       coffheader.f_nscns = 3;
  3538.       /* store an unlikely time so programs can
  3539.        * tell that there is a bsd header
  3540.        */
  3541.       coffheader.f_timdat = 1;
  3542.       coffheader.f_symptr = 0;
  3543.       coffheader.f_nsyms = 0;
  3544.       coffheader.f_opthdr = 28;
  3545.       coffheader.f_flags = 0x103;
  3546.       /* aouthdr */
  3547.       coffheader.magic = ZMAGIC;
  3548.       coffheader.vstamp = 0;
  3549.       coffheader.tsize = tp->s_size;
  3550.       coffheader.dsize = dp->s_size;
  3551.       coffheader.bsize = bp->s_size;
  3552.       coffheader.entry = outheader.a_entry;
  3553.       coffheader.text_start = tp->s_vaddr;
  3554.       coffheader.data_start = dp->s_vaddr;
  3555.     }
  3556. #endif
  3557.  
  3558. #ifdef INITIALIZE_HEADER
  3559.   INITIALIZE_HEADER;
  3560. #endif
  3561.  
  3562.   if (strip_symbols == STRIP_ALL)
  3563.     nsyms = 0;
  3564.   else
  3565.     {
  3566.       nsyms = (defined_global_sym_count
  3567.            + undefined_global_sym_count);
  3568.       if (discard_locals == DISCARD_L)
  3569.     nsyms += non_L_local_sym_count;
  3570.       else if (discard_locals == DISCARD_NONE)
  3571.     nsyms += local_sym_count;
  3572.       /* One extra for following reference on indirects */
  3573.       if (relocatable_output)
  3574.     nsyms += set_symbol_count + global_indirect_count;
  3575.     }
  3576.  
  3577.   if (strip_symbols == STRIP_NONE)
  3578.     nsyms += debugger_sym_count;
  3579.  
  3580.   outheader.a_syms = nsyms * sizeof (struct nlist);
  3581.  
  3582.   if (relocatable_output)
  3583.     {
  3584.       outheader.a_trsize = text_reloc_size;
  3585.       outheader.a_drsize = data_reloc_size;
  3586.     }
  3587.   else
  3588.     {
  3589.       outheader.a_trsize = 0;
  3590.       outheader.a_drsize = 0;
  3591.     }
  3592.  
  3593. #ifdef COFF_ENCAPSULATE
  3594.   if (need_coff_header)
  3595.     mywrite (&coffheader, sizeof coffheader, 1, outdesc);
  3596. #endif
  3597. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  3598.   fix_exec_header_byte_order(&outheader);
  3599. #endif
  3600.   mywrite (&outheader, sizeof (struct exec), 1, outdesc);
  3601. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  3602.   fix_exec_header_byte_order(&outheader);
  3603. #endif
  3604.  
  3605.   /* Output whatever padding is required in the executable file
  3606.      between the header and the start of the text.  */
  3607.  
  3608. #ifndef COFF_ENCAPSULATE
  3609.   padfile (N_TXTOFF (outheader) - sizeof outheader, outdesc);
  3610. #endif
  3611. }
  3612.  
  3613. /* Relocate the text segment of each input file
  3614.    and write to the output file.  */
  3615.  
  3616. void
  3617. write_text ()
  3618. {
  3619.   if (trace_files)
  3620.     fprintf (stderr, "Copying and relocating text:\n\n");
  3621.  
  3622.   each_full_file (copy_text);
  3623.   file_close ();
  3624.  
  3625.   if (trace_files)
  3626.     fprintf (stderr, "\n");
  3627.  
  3628.   padfile (text_pad, outdesc);
  3629. }
  3630.  
  3631. int
  3632. text_offset (entry)
  3633.      struct file_entry *entry;
  3634. {
  3635.   return entry->starting_offset + N_TXTOFF (entry->header);
  3636. }
  3637.  
  3638. /* Read in all of the relocation information */
  3639.  
  3640. void
  3641. read_relocation ()
  3642. {
  3643.   each_full_file (read_file_relocation);
  3644. }
  3645.  
  3646. /* Read in the relocation sections of ENTRY if necessary */
  3647.  
  3648. void
  3649. read_file_relocation (entry)
  3650.      struct file_entry *entry;
  3651. {
  3652.   register struct relocation_info *reloc;
  3653.   int desc;
  3654.   int read_return;
  3655.  
  3656.   desc = -1;
  3657.   if (!entry->textrel)
  3658.     {
  3659.       reloc = (struct relocation_info *) xmalloc (entry->header.a_trsize);
  3660.       desc = file_open (entry);
  3661.       lseek (desc,
  3662.          text_offset (entry) + entry->header.a_text + entry->header.a_data,
  3663.          L_SET);
  3664.       if (entry->header.a_trsize != (read_return = read (desc, reloc, entry->header.a_trsize)))
  3665.     {
  3666.       fprintf (stderr, "Return from read: %d\n", read_return);
  3667.       fatal_with_file ("premature eof in text relocation of ", entry);
  3668.     }
  3669. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  3670.       target_to_host_reloc_byte_order(reloc,
  3671.     entry->header.a_trsize/sizeof(*reloc));
  3672. #endif
  3673.       entry->textrel = reloc;
  3674.     }
  3675.  
  3676.   if (!entry->datarel)
  3677.     {
  3678.       reloc = (struct relocation_info *) xmalloc (entry->header.a_drsize);
  3679.       if (desc == -1) desc = file_open (entry);
  3680.       lseek (desc,
  3681.          text_offset (entry) + entry->header.a_text
  3682.          + entry->header.a_data + entry->header.a_trsize,
  3683.          L_SET);
  3684.       if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  3685.     fatal_with_file ("premature eof in data relocation of ", entry);
  3686. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  3687.       target_to_host_reloc_byte_order(reloc,
  3688.     entry->header.a_drsize/sizeof(*reloc));
  3689. #endif
  3690.       entry->datarel = reloc;
  3691.     }
  3692. }
  3693.  
  3694. /* Read the text segment contents of ENTRY, relocate them,
  3695.    and write the result to the output file.
  3696.    If `-r', save the text relocation for later reuse.  */
  3697.  
  3698. void
  3699. copy_text (entry)
  3700.      struct file_entry *entry;
  3701. {
  3702.   register char *bytes;
  3703.   register int desc;
  3704.   register struct relocation_info *reloc;
  3705.  
  3706.   if (trace_files)
  3707.     prline_file_name (entry, stderr);
  3708.  
  3709.   desc = file_open (entry);
  3710.  
  3711.   /* Allocate space for the file's text section */
  3712.  
  3713.   bytes = (char *) alloca (entry->header.a_text);
  3714.  
  3715.   /* Deal with relocation information however is appropriate */
  3716.  
  3717.   if (entry->textrel)  reloc = entry->textrel;
  3718.   else if (relocatable_output)
  3719.     {
  3720.       read_file_relocation (entry);
  3721.       reloc = entry->textrel;
  3722.     }
  3723.   else
  3724.     {
  3725.       reloc = (struct relocation_info *) alloca (entry->header.a_trsize);
  3726.       lseek (desc, text_offset (entry) + entry->header.a_text + entry->header.a_data, 0);
  3727.       if (entry->header.a_trsize != read (desc, reloc, entry->header.a_trsize))
  3728.     fatal_with_file ("premature eof in text relocation of ", entry);
  3729. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  3730.       target_to_host_reloc_byte_order(reloc,
  3731.       entry->header.a_trsize/sizeof(*reloc));
  3732. #endif
  3733.     }
  3734.  
  3735.   /* Read the text section into core.  */
  3736.  
  3737.   lseek (desc, text_offset (entry), 0);
  3738.   if (entry->header.a_text != read (desc, bytes, entry->header.a_text))
  3739.     fatal_with_file ("premature eof in text section of ", entry);
  3740.  
  3741.  
  3742.   /* Relocate the text according to the text relocation.  */
  3743.  
  3744.   perform_relocation (bytes, entry->text_start_address, entry->header.a_text,
  3745.               reloc, entry->header.a_trsize, entry);
  3746.  
  3747.   /* Write the relocated text to the output file.  */
  3748.  
  3749.   mywrite (bytes, 1, entry->header.a_text, outdesc);
  3750. }
  3751.  
  3752. /* Relocate the data segment of each input file
  3753.    and write to the output file.  */
  3754.  
  3755. void
  3756. write_data ()
  3757. {
  3758.   if (trace_files)
  3759.     fprintf (stderr, "Copying and relocating data:\n\n");
  3760.  
  3761.   each_full_file (copy_data);
  3762.   file_close ();
  3763.  
  3764.   /* Write out the set element vectors.  See digest symbols for
  3765.      description of length of the set vector section.  */
  3766.  
  3767. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  3768.   {
  3769.     int i;
  3770.     for (i = 0; i < 2 * set_symbol_count + set_vector_count; ++i) {
  3771.     fix_byte_order(&set_vectors[i], sizeof(*set_vectors));
  3772.     }
  3773.   }
  3774. #endif
  3775.  
  3776.   if (set_vector_count)
  3777.     mywrite (set_vectors, 2 * set_symbol_count + set_vector_count,
  3778.          sizeof (unsigned long), outdesc);
  3779.  
  3780.   if (trace_files)
  3781.     fprintf (stderr, "\n");
  3782.  
  3783.   padfile (data_pad, outdesc);
  3784. }
  3785.  
  3786. /* Read the data segment contents of ENTRY, relocate them,
  3787.    and write the result to the output file.
  3788.    If `-r', save the data relocation for later reuse.
  3789.    See comments in `copy_text'.  */
  3790.  
  3791. void
  3792. copy_data (entry)
  3793.      struct file_entry *entry;
  3794. {
  3795.   register struct relocation_info *reloc;
  3796.   register char *bytes;
  3797.   register int desc;
  3798.  
  3799.   if (trace_files)
  3800.     prline_file_name (entry, stderr);
  3801.  
  3802.   desc = file_open (entry);
  3803.  
  3804.   bytes = (char *) alloca (entry->header.a_data);
  3805.  
  3806.   if (entry->datarel) reloc = entry->datarel;
  3807.   else if (relocatable_output)    /* Will need this again */
  3808.     {
  3809.       read_file_relocation (entry);
  3810.       reloc = entry->datarel;
  3811.     }
  3812.   else
  3813.     {
  3814.       reloc = (struct relocation_info *) alloca (entry->header.a_drsize);
  3815.       lseek (desc, text_offset (entry) + entry->header.a_text
  3816.          + entry->header.a_data + entry->header.a_trsize,
  3817.          0);
  3818.       if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  3819.     fatal_with_file ("premature eof in data relocation of ", entry);
  3820. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  3821.       target_to_host_reloc_byte_order(reloc,
  3822.       entry->header.a_drsize/sizeof(*reloc));
  3823. #endif
  3824.     }
  3825.  
  3826.   lseek (desc, text_offset (entry) + entry->header.a_text, 0);
  3827.   if (entry->header.a_data != read (desc, bytes, entry->header.a_data))
  3828.     fatal_with_file ("premature eof in data section of ", entry);
  3829.  
  3830.   perform_relocation (bytes, entry->data_start_address - entry->header.a_text,
  3831.               entry->header.a_data, reloc, entry->header.a_drsize, entry);
  3832.  
  3833.   mywrite (bytes, 1, entry->header.a_data, outdesc);
  3834. }
  3835.  
  3836. /* Relocate ENTRY's text or data section contents.
  3837.    DATA is the address of the contents, in core.
  3838.    DATA_SIZE is the length of the contents.
  3839.    PC_RELOCATION is the difference between the address of the contents
  3840.      in the output file and its address in the input file.
  3841.    RELOC_INFO is the address of the relocation info, in core.
  3842.    RELOC_SIZE is its length in bytes.  */
  3843. /* This version is about to be severley hacked by Randy.  Hope it
  3844.    works afterwards. */
  3845. void
  3846. perform_relocation (data, pc_relocation, data_size, reloc_info, reloc_size, entry)
  3847.      char *data;
  3848.      struct relocation_info *reloc_info;
  3849.      struct file_entry *entry;
  3850.      int pc_relocation;
  3851.      int data_size;
  3852.      int reloc_size;
  3853. {
  3854.   register struct relocation_info *p = reloc_info;
  3855.   struct relocation_info *end
  3856.     = reloc_info + reloc_size / sizeof (struct relocation_info);
  3857.   int text_relocation = entry->text_start_address;
  3858.   int data_relocation = entry->data_start_address - entry->header.a_text;
  3859.   int bss_relocation
  3860.     = entry->bss_start_address - entry->header.a_text - entry->header.a_data;
  3861.  
  3862.   for (; p < end; p++)
  3863.     {
  3864.       register int relocation = 0;
  3865.       register int addr = RELOC_ADDRESS(p);
  3866.       register unsigned int mask = 0;
  3867.       register unsigned int x;
  3868.  
  3869.       if (addr >= data_size)
  3870.     fatal_with_file ("relocation address out of range in ", entry);
  3871.  
  3872.       if (RELOC_EXTERN_P(p))
  3873.     {
  3874.       int symindex = RELOC_SYMBOL (p) * sizeof (struct nlist);
  3875.       symbol *sp = ((symbol *)
  3876.             (((struct nlist *)
  3877.               (((char *)entry->symbols) + symindex))
  3878.              ->n_un.n_name));
  3879.  
  3880. #ifdef N_INDR
  3881.       /* Resolve indirection */
  3882.       if ((sp->defined & ~N_EXT) == N_INDR)
  3883.         sp = (symbol *) sp->value;
  3884. #endif
  3885.  
  3886.       if (symindex >= entry->header.a_syms)
  3887.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  3888.  
  3889.       /* If the symbol is undefined, leave it at zero.  */
  3890.       if (! sp->defined)
  3891.         relocation = 0;
  3892.       else
  3893.         relocation = sp->value;
  3894.     }
  3895.       else switch (RELOC_TYPE(p))
  3896.     {
  3897.     case N_TEXT:
  3898.     case N_TEXT | N_EXT:
  3899.       relocation = text_relocation;
  3900.       break;
  3901.  
  3902.     case N_DATA:
  3903.     case N_DATA | N_EXT:
  3904.       /* A word that points to beginning of the the data section
  3905.          initially contains not 0 but rather the "address" of that section
  3906.          in the input file, which is the length of the file's text.  */
  3907.       relocation = data_relocation;
  3908.       break;
  3909.  
  3910.     case N_BSS:
  3911.     case N_BSS | N_EXT:
  3912.       /* Similarly, an input word pointing to the beginning of the bss
  3913.          initially contains the length of text plus data of the file.  */
  3914.       relocation = bss_relocation;
  3915.       break;
  3916.  
  3917.     case N_ABS:
  3918.     case N_ABS | N_EXT:
  3919.       /* Don't know why this code would occur, but apparently it does.  */
  3920.       break;
  3921.  
  3922.     default:
  3923.       fatal_with_file ("nonexternal relocation code invalid in ", entry);
  3924.     }
  3925.  
  3926.       if (RELOC_PCREL_P(p))
  3927.     relocation -= pc_relocation;
  3928.  
  3929. #ifdef RELOC_ADD_EXTRA
  3930.       relocation += RELOC_ADD_EXTRA(p);
  3931.       if (relocatable_output)
  3932.     {
  3933.       /* If this RELOC_ADD_EXTRA is 0, it means that the
  3934.          symbol was external and the relocation does not
  3935.          need a fixup here.  */
  3936.       if (RELOC_ADD_EXTRA (p))
  3937.         {
  3938.           if (! RELOC_PCREL_P (p))
  3939.         RELOC_ADD_EXTRA (p) = relocation;
  3940.           else
  3941.         RELOC_ADD_EXTRA (p) -= pc_relocation;
  3942.         }
  3943. #if 0
  3944.       if (! RELOC_PCREL_P (p))
  3945.         {
  3946.           if ((int)p->r_type <= RELOC_32
  3947.           || RELOC_EXTERN_P (p) == 0)
  3948.         RELOC_ADD_EXTRA (p) = relocation;
  3949.         }
  3950.       else if (RELOC_EXTERN_P (p))
  3951.         RELOC_ADD_EXTRA (p) -= pc_relocation;
  3952. #endif
  3953.       continue;
  3954.     }
  3955. #endif
  3956.  
  3957.       relocation >>= RELOC_VALUE_RIGHTSHIFT(p);
  3958.  
  3959.       /* Unshifted mask for relocation */
  3960.       mask = 1 << RELOC_TARGET_BITSIZE(p) - 1;
  3961.       mask |= mask - 1;
  3962.       relocation &= mask;
  3963.  
  3964.       /* Shift everything up to where it's going to be used */
  3965.       relocation <<= RELOC_TARGET_BITPOS(p);
  3966.       mask <<= RELOC_TARGET_BITPOS(p);
  3967.  
  3968.       switch (RELOC_TARGET_SIZE(p))
  3969.     {
  3970.     case 0:
  3971.       if (RELOC_MEMORY_SUB_P(p))
  3972.         relocation -= mask & *(char *) (data + addr);
  3973.       else if (RELOC_MEMORY_ADD_P(p))
  3974.         relocation += mask & *(char *) (data + addr);
  3975.       *(char *) (data + addr) &= ~mask;
  3976.       *(char *) (data + addr) |= relocation;
  3977.       break;
  3978.  
  3979.     case 1:
  3980. #if TARGET_BYTE_ORDER==BIG_ENDIAN
  3981.       x = ((unsigned char *) (data + addr))[1]
  3982.        | (((unsigned char *) (data + addr))[0] << 8);
  3983. #endif
  3984. #if TARGET_BYTE_ORDER==LITTLE_ENDIAN
  3985.       x = ((unsigned char *) (data + addr))[0]
  3986.        | (((unsigned char *) (data + addr))[1] << 8);
  3987. #endif
  3988.       if (RELOC_MEMORY_SUB_P(p))
  3989.         relocation -= mask & x;
  3990.       else if (RELOC_MEMORY_ADD_P(p))
  3991.         relocation += mask & x;
  3992.       x &= ~mask;
  3993.       x |= relocation;
  3994. #if TARGET_BYTE_ORDER==BIG_ENDIAN
  3995.       ((unsigned char *) (data + addr))[1] = x;
  3996.       ((unsigned char *) (data + addr))[0] = x >> 8;
  3997. #endif
  3998. #if TARGET_BYTE_ORDER==LITTLE_ENDIAN
  3999.       ((unsigned char *) (data + addr))[0] = x;
  4000.       ((unsigned char *) (data + addr))[1] = x >> 8;
  4001. #endif
  4002.       break;
  4003.  
  4004.     case 2:
  4005. #if TARGET_BYTE_ORDER==BIG_ENDIAN
  4006.       x = ((unsigned char *) (data + addr))[3]
  4007.        | (((unsigned char *) (data + addr))[2] << 8)
  4008.        | (((unsigned char *) (data + addr))[1] << 16)
  4009.        | (((unsigned char *) (data + addr))[0] << 24);
  4010. #endif
  4011. #if TARGET_BYTE_ORDER==LITTLE_ENDIAN
  4012.       x = ((unsigned char *) (data + addr))[0]
  4013.        | (((unsigned char *) (data + addr))[1] << 8)
  4014.        | (((unsigned char *) (data + addr))[2] << 16)
  4015.        | (((unsigned char *) (data + addr))[3] << 24);
  4016. #endif
  4017.           if (RELOC_MEMORY_SUB_P(p))
  4018.         relocation -= mask & x;
  4019.       else if (RELOC_MEMORY_ADD_P(p))
  4020.         relocation += mask & x;
  4021.       x &= ~mask;
  4022.       x |= relocation;
  4023. #if TARGET_BYTE_ORDER==BIG_ENDIAN
  4024.       ((unsigned char *) (data + addr))[3] = x;
  4025.       ((unsigned char *) (data + addr))[2] = x >> 8;
  4026.       ((unsigned char *) (data + addr))[1] = x >> 16;
  4027.       ((unsigned char *) (data + addr))[0] = x >> 24;
  4028. #endif
  4029. #if TARGET_BYTE_ORDER==LITTLE_ENDIAN
  4030.       ((unsigned char *) (data + addr))[0] = x;
  4031.       ((unsigned char *) (data + addr))[1] = x >> 8;
  4032.       ((unsigned char *) (data + addr))[2] = x >> 16;
  4033.       ((unsigned char *) (data + addr))[3] = x >> 24;
  4034. #endif
  4035.       break;
  4036.  
  4037.     default:
  4038.       fatal_with_file ("Unimplemented relocation field length in ", entry);
  4039.     }
  4040.     }
  4041. }
  4042.  
  4043. /* For relocatable_output only: write out the relocation,
  4044.    relocating the addresses-to-be-relocated.  */
  4045.  
  4046. void coptxtrel (), copdatrel ();
  4047.  
  4048. void
  4049. write_rel ()
  4050. {
  4051.   register int i;
  4052.   register int count = 0;
  4053.  
  4054.   if (trace_files)
  4055.     fprintf (stderr, "Writing text relocation:\n\n");
  4056.  
  4057.   /* Assign each global symbol a sequence number, giving the order
  4058.      in which `write_syms' will write it.
  4059.      This is so we can store the proper symbolnum fields
  4060.      in relocation entries we write.  */
  4061.  
  4062.   for (i = 0; i < TABSIZE; i++)
  4063.     {
  4064.       symbol *sp;
  4065.       for (sp = symtab[i]; sp; sp = sp->link)
  4066.     if (sp->referenced || sp->defined)
  4067.       {
  4068.         sp->def_count = count++;
  4069.         /* Leave room for the reference required by N_INDR, if
  4070.            necessary.  */
  4071.         if ((sp->defined & ~N_EXT) == N_INDR)
  4072.           count++;
  4073.       }
  4074.     }
  4075.   /* Correct, because if (reloatable_output), we will also be writing
  4076.      whatever indirect blocks we have.  */
  4077.   if (count != defined_global_sym_count
  4078.       + undefined_global_sym_count + global_indirect_count)
  4079.     fatal ("internal error");
  4080.  
  4081.   /* Write out the relocations of all files, remembered from copy_text.  */
  4082.  
  4083.   each_full_file (coptxtrel);
  4084.  
  4085.   if (trace_files)
  4086.     fprintf (stderr, "\nWriting data relocation:\n\n");
  4087.  
  4088.   each_full_file (copdatrel);
  4089.  
  4090.   if (trace_files)
  4091.     fprintf (stderr, "\n");
  4092. }
  4093.  
  4094. void
  4095. coptxtrel (entry)
  4096.      struct file_entry *entry;
  4097. {
  4098.   register struct relocation_info *p, *end;
  4099.   register int reloc = entry->text_start_address;
  4100.  
  4101.   p = entry->textrel;
  4102.   end = (struct relocation_info *) (entry->header.a_trsize + (char *) p);
  4103.   while (p < end)
  4104.     {
  4105.       RELOC_ADDRESS(p) += reloc;
  4106.       if (RELOC_EXTERN_P(p))
  4107.     {
  4108.       register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
  4109.       symbol *symptr = ((symbol *)
  4110.                 (((struct nlist *)
  4111.                   (((char *)entry->symbols) + symindex))
  4112.                  ->n_un.n_name));
  4113.  
  4114.       if (symindex >= entry->header.a_syms)
  4115.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  4116.  
  4117. #ifdef N_INDR
  4118.       /* Resolve indirection.  */
  4119.       if ((symptr->defined & ~N_EXT) == N_INDR)
  4120.         symptr = (symbol *) symptr->value;
  4121. #endif
  4122.  
  4123.       /* If the symbol is now defined, change the external relocation
  4124.          to an internal one.  */
  4125.  
  4126.       if (symptr->defined)
  4127.         {
  4128.           RELOC_EXTERN_P(p) = 0;
  4129.           RELOC_SYMBOL(p) = (symptr->defined & N_TYPE);
  4130. #ifdef RELOC_ADD_EXTRA
  4131.           /* If we aren't going to be adding in the value in
  4132.              memory on the next pass of the loader, then we need
  4133.          to add it in from the relocation entry.  Otherwise
  4134.              the work we did in this pass is lost.  */
  4135.           if (!RELOC_MEMORY_ADD_P(p))
  4136.         RELOC_ADD_EXTRA (p) += symptr->value;
  4137. #endif
  4138.         }
  4139.       else
  4140.         /* Debugger symbols come first, so have to start this
  4141.            after them.  */
  4142.           RELOC_SYMBOL(p) = (symptr->def_count + nsyms
  4143.                  - defined_global_sym_count
  4144.                  - undefined_global_sym_count
  4145.                  - global_indirect_count);
  4146.     }
  4147.       p++;
  4148.     }
  4149. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4150.     host_to_target_reloc_byte_order((struct relocation_info *) entry->textrel,
  4151.     entry->header.a_trsize/sizeof(struct relocation_info));
  4152. #endif
  4153.   mywrite (entry->textrel, 1, entry->header.a_trsize, outdesc);
  4154. }
  4155.  
  4156. void
  4157. copdatrel (entry)
  4158.      struct file_entry *entry;
  4159. {
  4160.   register struct relocation_info *p, *end;
  4161.   /* Relocate the address of the relocation.
  4162.      Old address is relative to start of the input file's data section.
  4163.      New address is relative to start of the output file's data section.  */
  4164.   register int reloc = entry->data_start_address - text_size;
  4165.  
  4166.   p = entry->datarel;
  4167.   end = (struct relocation_info *) (entry->header.a_drsize + (char *) p);
  4168.   while (p < end)
  4169.     {
  4170.       RELOC_ADDRESS(p) += reloc;
  4171.       if (RELOC_EXTERN_P(p))
  4172.     {
  4173.       register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
  4174.       symbol *symptr = ((symbol *)
  4175.                 (((struct nlist *)
  4176.                   (((char *)entry->symbols) + symindex))
  4177.                  ->n_un.n_name));
  4178.       int symtype;
  4179.  
  4180.       if (symindex >= entry->header.a_syms)
  4181.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  4182.  
  4183. #ifdef N_INDR
  4184.       /* Resolve indirection.  */
  4185.       if ((symptr->defined & ~N_EXT) == N_INDR)
  4186.         symptr = (symbol *) symptr->value;
  4187. #endif
  4188.  
  4189.        symtype = symptr->defined & N_TYPE;
  4190.  
  4191.       if (force_common_definition
  4192.           || symtype == N_DATA || symtype == N_TEXT || symtype == N_ABS)
  4193.         {
  4194.           RELOC_EXTERN_P(p) = 0;
  4195.           RELOC_SYMBOL(p) = symtype;
  4196.         }
  4197.       else
  4198.         /* Debugger symbols come first, so have to start this
  4199.            after them.  */
  4200.         RELOC_SYMBOL(p)
  4201.           = (((symbol *)
  4202.           (((struct nlist *)
  4203.             (((char *)entry->symbols) + symindex))
  4204.            ->n_un.n_name))
  4205.          ->def_count
  4206.          + nsyms - defined_global_sym_count
  4207.          - undefined_global_sym_count
  4208.          - global_indirect_count);
  4209.     }
  4210.       p++;
  4211.     }
  4212. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4213.     host_to_target_reloc_byte_order((struct relocation_info *) entry->datarel,
  4214.     entry->header.a_drsize/sizeof(struct relocation_info));
  4215. #endif
  4216.   mywrite (entry->datarel, 1, entry->header.a_drsize, outdesc);
  4217. }
  4218.  
  4219. void write_file_syms ();
  4220. void write_string_table ();
  4221.  
  4222. /* Offsets and current lengths of symbol and string tables in output file. */
  4223.  
  4224. int symbol_table_offset;
  4225. int symbol_table_len;
  4226.  
  4227. /* Address in output file where string table starts.  */
  4228. int string_table_offset;
  4229.  
  4230. /* Offset within string table
  4231.    where the strings in `strtab_vector' should be written.  */
  4232. int string_table_len;
  4233.  
  4234. /* Total size of string table strings allocated so far,
  4235.    including strings in `strtab_vector'.  */
  4236. int strtab_size;
  4237.  
  4238. /* Vector whose elements are strings to be added to the string table.  */
  4239. char **strtab_vector;
  4240.  
  4241. /* Vector whose elements are the lengths of those strings.  */
  4242. int *strtab_lens;
  4243.  
  4244. /* Index in `strtab_vector' at which the next string will be stored.  */
  4245. int strtab_index;
  4246.  
  4247. /* Add the string NAME to the output file string table.
  4248.    Record it in `strtab_vector' to be output later.
  4249.    Return the index within the string table that this string will have.  */
  4250.  
  4251. int
  4252. assign_string_table_index (name)
  4253.      char *name;
  4254. {
  4255.   register int index = strtab_size;
  4256.   register int len = strlen (name) + 1;
  4257.  
  4258.   strtab_size += len;
  4259.   strtab_vector[strtab_index] = name;
  4260.   strtab_lens[strtab_index++] = len;
  4261.  
  4262.   return index;
  4263. }
  4264.  
  4265. FILE *outstream = (FILE *) 0;
  4266.  
  4267. /* Write the contents of `strtab_vector' into the string table.
  4268.    This is done once for each file's local&debugger symbols
  4269.    and once for the global symbols.  */
  4270.  
  4271. void
  4272. write_string_table ()
  4273. {
  4274.   register int i;
  4275.  
  4276.   lseek (outdesc, string_table_offset + string_table_len, 0);
  4277.  
  4278.   if (!outstream)
  4279.     outstream = fdopen (outdesc, "w");
  4280.  
  4281.   for (i = 0; i < strtab_index; i++)
  4282.     {
  4283.       fwrite (strtab_vector[i], 1, strtab_lens[i], outstream);
  4284.       string_table_len += strtab_lens[i];
  4285.     }
  4286.  
  4287.   fflush (outstream);
  4288.  
  4289.   /* Report I/O error such as disk full.  */
  4290.   if (ferror (outstream))
  4291.     perror_name (output_filename);
  4292. }
  4293.  
  4294. /* Write the symbol table and string table of the output file.  */
  4295.  
  4296. void
  4297. write_syms ()
  4298. {
  4299.   /* Number of symbols written so far.  */
  4300.   int syms_written = 0;
  4301.   register int i;
  4302.   register symbol *sp;
  4303.  
  4304.   /* Buffer big enough for all the global symbols.  One
  4305.      extra struct for each indirect symbol to hold the extra reference
  4306.      following. */
  4307.   struct nlist *buf
  4308.     = (struct nlist *) alloca ((defined_global_sym_count
  4309.                 + undefined_global_sym_count
  4310.                 + global_indirect_count)
  4311.                    * sizeof (struct nlist));
  4312.   /* Pointer for storing into BUF.  */
  4313.   register struct nlist *bufp = buf;
  4314.  
  4315.   /* Size of string table includes the bytes that store the size.  */
  4316.   strtab_size = sizeof strtab_size;
  4317.  
  4318.   symbol_table_offset = N_SYMOFF (outheader);
  4319.   symbol_table_len = 0;
  4320.   string_table_offset = N_STROFF (outheader);
  4321.   string_table_len = strtab_size;
  4322.  
  4323.   if (strip_symbols == STRIP_ALL)
  4324.     return;
  4325.  
  4326.   /* Write the local symbols defined by the various files.  */
  4327.  
  4328.   each_file (write_file_syms, &syms_written);
  4329.   file_close ();
  4330.  
  4331.   /* Now write out the global symbols.  */
  4332.  
  4333.   /* Allocate two vectors that record the data to generate the string
  4334.      table from the global symbols written so far.  This must include
  4335.      extra space for the references following indirect outputs. */
  4336.  
  4337.   strtab_vector = (char **) alloca ((num_hash_tab_syms
  4338.                      + global_indirect_count) * sizeof (char *));
  4339.   strtab_lens = (int *) alloca ((num_hash_tab_syms
  4340.                  + global_indirect_count) * sizeof (int));
  4341.   strtab_index = 0;
  4342.  
  4343.   /* Scan the symbol hash table, bucket by bucket.  */
  4344.  
  4345.   for (i = 0; i < TABSIZE; i++)
  4346.     for (sp = symtab[i]; sp; sp = sp->link)
  4347.       {
  4348.     struct nlist nl;
  4349.  
  4350.     nl.n_other = 0;
  4351.     nl.n_desc = 0;
  4352.  
  4353.     /* Compute a `struct nlist' for the symbol.  */
  4354.  
  4355.     if (sp->defined || sp->referenced)
  4356.       {
  4357.         /* common condition needs to be before undefined condition */
  4358.         /* because unallocated commons are set undefined in */
  4359.         /* digest_symbols */
  4360.         if (sp->defined > 1) /* defined with known type */
  4361.           {
  4362.         /* If the target of an indirect symbol has been
  4363.            defined and we are outputting an executable,
  4364.            resolve the indirection; it's no longer needed */
  4365.         if (!relocatable_output
  4366.             && ((sp->defined & N_TYPE) == N_INDR)
  4367.             && (((symbol *) sp->value)->defined > 1))
  4368.           {
  4369.             symbol *newsp = (symbol *) sp->value;
  4370.             nl.n_type = newsp->defined;
  4371.             nl.n_value = newsp->value;
  4372.           }
  4373.         else
  4374.           {
  4375.             nl.n_type = sp->defined;
  4376.             if (sp->defined != (N_INDR | N_EXT))
  4377.               nl.n_value = sp->value;
  4378.             else
  4379.               nl.n_value = 0;
  4380.           }
  4381.           }
  4382.         else if (sp->max_common_size) /* defined as common but not allocated. */
  4383.           {
  4384.         /* happens only with -r and not -d */
  4385.         /* write out a common definition */
  4386.         nl.n_type = N_UNDF | N_EXT;
  4387.         nl.n_value = sp->max_common_size;
  4388.           }
  4389.         else if (!sp->defined)          /* undefined -- legit only if -r */
  4390.           {
  4391.         nl.n_type = N_UNDF | N_EXT;
  4392.         nl.n_value = 0;
  4393.           }
  4394.         else
  4395.           fatal ("internal error: %s defined in mysterious way", sp->name);
  4396.  
  4397.         /* Allocate string table space for the symbol name.  */
  4398.  
  4399.         nl.n_un.n_strx = assign_string_table_index (sp->name);
  4400.  
  4401.         /* Output to the buffer and count it.  */
  4402.  
  4403.         *bufp++ = nl;
  4404.         syms_written++;
  4405.         if (nl.n_type == (N_INDR | N_EXT))
  4406.           {
  4407.         struct nlist xtra_ref;
  4408.         xtra_ref.n_type == N_EXT | N_UNDF;
  4409.         xtra_ref.n_un.n_strx
  4410.           = assign_string_table_index (((symbol *) sp->value)->name);
  4411.         xtra_ref.n_other = 0;
  4412.         xtra_ref.n_desc = 0;
  4413.         xtra_ref.n_value = 0;
  4414.         *bufp++ = xtra_ref;
  4415.         syms_written++;
  4416.           }
  4417.       }
  4418.       }
  4419.  
  4420.   /* Output the buffer full of `struct nlist's.  */
  4421.  
  4422.   lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  4423. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4424.   {
  4425.     int i;
  4426.  
  4427.     for (i = 0; i < bufp - buf; ++i)
  4428.       {
  4429.         fix_byte_order(&buf[i].n_un.n_name, sizeof(buf->n_un.n_name));
  4430.     fix_byte_order(&buf[i].n_desc, sizeof(buf->n_desc));
  4431.     fix_byte_order(&buf[i].n_value, sizeof(buf->n_value));
  4432.       }
  4433.   }
  4434. #endif
  4435.   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  4436.   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  4437.  
  4438.   if (syms_written != nsyms)
  4439.     fatal ("internal error: wrong number of symbols written into output file", 0);
  4440.  
  4441.   if (symbol_table_offset + symbol_table_len != string_table_offset)
  4442.     fatal ("internal error: inconsistent symbol table length", 0);
  4443.  
  4444.   /* Now the total string table size is known, so write it.
  4445.      We are already positioned at the right place in the file.  */
  4446.  
  4447. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4448.   fix_byte_order(&strtab_size, sizeof(strtab_size));
  4449. #endif
  4450.  
  4451.   mywrite (&strtab_size, sizeof (int), 1, outdesc);  /* we're at right place */
  4452.  
  4453.   /* Write the strings for the global symbols.  */
  4454.  
  4455.   write_string_table ();
  4456. }
  4457.  
  4458. /* Write the local and debugger symbols of file ENTRY.
  4459.    Increment *SYMS_WRITTEN_ADDR for each symbol that is written.  */
  4460.  
  4461. /* Note that we do not combine identical names of local symbols.
  4462.    dbx or gdb would be confused if we did that.  */
  4463.  
  4464. void
  4465. write_file_syms (entry, syms_written_addr)
  4466.      struct file_entry *entry;
  4467.      int *syms_written_addr;
  4468. {
  4469.   register struct nlist *p = entry->symbols;
  4470.   register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  4471.  
  4472.   /* Buffer to accumulate all the syms before writing them.
  4473.      It has one extra slot for the local symbol we generate here.  */
  4474.   struct nlist *buf
  4475.     = (struct nlist *) alloca (entry->header.a_syms + sizeof (struct nlist));
  4476.   register struct nlist *bufp = buf;
  4477.  
  4478.   /* Upper bound on number of syms to be written here.  */
  4479.   int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
  4480.  
  4481.   /* Make tables that record, for each symbol, its name and its name's length.
  4482.      The elements are filled in by `assign_string_table_index'.  */
  4483.  
  4484.   strtab_vector = (char **) alloca (max_syms * sizeof (char *));
  4485.   strtab_lens = (int *) alloca (max_syms * sizeof (int));
  4486.   strtab_index = 0;
  4487.  
  4488.   /* Generate a local symbol for the start of this file's text.  */
  4489.  
  4490.   if (discard_locals != DISCARD_ALL)
  4491.     {
  4492.       struct nlist nl;
  4493.  
  4494.       nl.n_type = N_TEXT;
  4495.       nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name);
  4496.       nl.n_value = entry->text_start_address;
  4497.       nl.n_desc = 0;
  4498.       nl.n_other = 0;
  4499.       *bufp++ = nl;
  4500.       (*syms_written_addr)++;
  4501.       entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
  4502.     }
  4503.  
  4504.   /* Read the file's string table.  */
  4505.  
  4506.   entry->strings = (char *) alloca (entry->string_size);
  4507.   read_entry_strings (file_open (entry), entry);
  4508.  
  4509.   for (; p < end; p++)
  4510.     {
  4511.       register int type = p->n_type;
  4512.       register int write = 0;
  4513.  
  4514.       /* WRITE gets 1 for a non-global symbol that should be written.  */
  4515.  
  4516.  
  4517.       if (SET_ELEMENT_P (type))    /* This occurs even if global.  These */
  4518.                 /* types of symbols are never written */
  4519.                 /* globally, though they are stored */
  4520.                 /* globally.  */
  4521.         write = relocatable_output;
  4522.       else if (!(type & (N_STAB | N_EXT)))
  4523.         /* ordinary local symbol */
  4524.     write = ((discard_locals != DISCARD_ALL)
  4525.          && !(discard_locals == DISCARD_L &&
  4526.               (p->n_un.n_strx + entry->strings)[0] == LPREFIX)
  4527.          && type != N_WARNING);
  4528.       else if (!(type & N_EXT))
  4529.     /* debugger symbol */
  4530.         write = (strip_symbols == STRIP_NONE);
  4531.  
  4532.       if (write)
  4533.     {
  4534.       /* If this symbol has a name,
  4535.          allocate space for it in the output string table.  */
  4536.  
  4537.       if (p->n_un.n_strx)
  4538.         p->n_un.n_strx = assign_string_table_index (p->n_un.n_strx
  4539.                             + entry->strings);
  4540.  
  4541.       /* Output this symbol to the buffer and count it.  */
  4542.  
  4543.       *bufp++ = *p;
  4544.       (*syms_written_addr)++;
  4545.     }
  4546.     }
  4547.  
  4548.   /* All the symbols are now in BUF; write them.  */
  4549.  
  4550.   lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  4551. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4552.   {
  4553.     int i;
  4554.  
  4555.     for (i = 0; i < bufp - buf; ++i)
  4556.       {
  4557.     fix_byte_order(&buf[i].n_un.n_name, sizeof(buf->n_un.n_name));
  4558.     fix_byte_order(&buf[i].n_desc, sizeof(buf->n_desc));
  4559.     fix_byte_order(&buf[i].n_value, sizeof(buf->n_value));
  4560.       }
  4561.   }
  4562. #endif
  4563.   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  4564.   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  4565.  
  4566.   /* Write the string-table data for the symbols just written,
  4567.      using the data in vectors `strtab_vector' and `strtab_lens'.  */
  4568.  
  4569.   write_string_table ();
  4570.   entry->strings = 0;        /* Since it will dissapear anyway.  */
  4571. }
  4572.  
  4573. /* Copy any GDB symbol segments from the input files to the output file.
  4574.    The contents of the symbol segment is copied without change
  4575.    except that we store some information into the beginning of it.  */
  4576.  
  4577. void write_file_symseg ();
  4578.  
  4579. void
  4580. write_symsegs ()
  4581. {
  4582.   each_file (write_file_symseg, 0);
  4583. }
  4584.  
  4585. void
  4586. write_file_symseg (entry)
  4587.      struct file_entry *entry;
  4588. {
  4589.   char buffer[4096];
  4590.   struct symbol_root root;
  4591.   int indesc;
  4592.   int len;
  4593.  
  4594.   if (entry->symseg_offset == 0)
  4595.     return;
  4596.  
  4597.   /* This entry has a symbol segment.  Read the root of the segment.  */
  4598.  
  4599.   indesc = file_open (entry);
  4600.   lseek (indesc, entry->symseg_offset + entry->starting_offset, 0);
  4601.   if (sizeof root != read (indesc, &root, sizeof root))
  4602.     fatal_with_file ("premature end of file in symbol segment of ", entry);
  4603.  
  4604. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4605.   fix_symbol_root_byte_order(&root);
  4606. #endif
  4607.  
  4608.   /* Store some relocation info into the root.  */
  4609.  
  4610.   root.ldsymoff = entry->local_syms_offset;
  4611.   root.textrel = entry->text_start_address;
  4612.   root.datarel = entry->data_start_address - entry->header.a_text;
  4613.   root.bssrel = entry->bss_start_address
  4614.     - entry->header.a_text - entry->header.a_data;
  4615.   root.databeg = entry->data_start_address - root.datarel;
  4616.   root.bssbeg = entry->bss_start_address - root.bssrel;
  4617.  
  4618.   /* Write the modified root into the output file.  */
  4619.  
  4620. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4621.   fix_symbol_root_byte_order(&root);
  4622. #endif
  4623.   mywrite (&root, sizeof root, 1, outdesc);
  4624.  
  4625.   /* Copy the rest of the symbol segment unchanged.  */
  4626.  
  4627.   if (entry->superfile)
  4628.     {
  4629.       /* Library member: number of bytes to copy is determined
  4630.      from the member's total size.  */
  4631.  
  4632.       int total = entry->total_size - entry->symseg_offset - sizeof root;
  4633.  
  4634.       while (total > 0)
  4635.     {
  4636.       len = read (indesc, buffer, min (sizeof buffer, total));
  4637.  
  4638.       if (len != min (sizeof buffer, total))
  4639.         fatal_with_file ("premature end of file in symbol segment of ", entry);
  4640.       total -= len;
  4641.       mywrite (buffer, len, 1, outdesc);
  4642.     }
  4643.     }
  4644.   else
  4645.     {
  4646.       /* A separate file: copy until end of file.  */
  4647.  
  4648.       while (len = read (indesc, buffer, sizeof buffer))
  4649.     {
  4650.       mywrite (buffer, len, 1, outdesc);
  4651.       if (len < sizeof buffer)
  4652.         break;
  4653.     }
  4654.     }
  4655.  
  4656.   file_close ();
  4657. }
  4658.  
  4659. /* Create the symbol table entries for `etext', `edata' and `end'.  */
  4660.  
  4661. void
  4662. symtab_init ()
  4663. {
  4664. #ifndef nounderscore
  4665.   edata_symbol = getsym ("_edata");
  4666.   etext_symbol = getsym ("_etext");
  4667.   end_symbol = getsym ("_end");
  4668. #else
  4669.   edata_symbol = getsym ("edata");
  4670.   etext_symbol = getsym ("etext");
  4671.   end_symbol = getsym ("end");
  4672. #endif
  4673.  
  4674. #if TARGET_MACHINE==TARGET_SUN4 || TARGET_MACHINE==TARGET_SUN3 || TARGET_MACHINE==TARGET_SUN2
  4675.   {
  4676.     symbol *dynamic_symbol = getsym ("__DYNAMIC");
  4677.     dynamic_symbol->defined = N_ABS | N_EXT;
  4678.     dynamic_symbol->referenced = 1;
  4679.     dynamic_symbol->value = 0;
  4680.   }
  4681. #endif
  4682. #if TARGET_MACHINE==TARGET_SEQUENT
  4683.   {
  4684.     symbol *_387_flt_symbol = getsym ("_387_flt");
  4685.     _387_flt_symbol->defined = N_ABS | N_EXT;
  4686.     _387_flt_symbol->referenced = 1;
  4687.     _387_flt_symbol->value = 0;
  4688.   }
  4689. #endif
  4690.  
  4691.   edata_symbol->defined = N_DATA | N_EXT;
  4692.   etext_symbol->defined = N_TEXT | N_EXT;
  4693.   end_symbol->defined = N_BSS | N_EXT;
  4694.  
  4695.   edata_symbol->referenced = 1;
  4696.   etext_symbol->referenced = 1;
  4697.   end_symbol->referenced = 1;
  4698. }
  4699.  
  4700. /* Compute the hash code for symbol name KEY.  */
  4701.  
  4702. int
  4703. hash_string (key)
  4704.      char *key;
  4705. {
  4706.   register char *cp;
  4707.   register int k;
  4708.  
  4709.   cp = key;
  4710.   k = 0;
  4711.   while (*cp)
  4712.     k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
  4713.  
  4714.   return k;
  4715. }
  4716.  
  4717. /* Get the symbol table entry for the global symbol named KEY.
  4718.    Create one if there is none.  */
  4719.  
  4720. symbol *
  4721. getsym (key)
  4722.      char *key;
  4723. {
  4724.   register int hashval;
  4725.   register symbol *bp;
  4726.  
  4727.   /* Determine the proper bucket.  */
  4728.  
  4729.   hashval = hash_string (key) % TABSIZE;
  4730.  
  4731.   /* Search the bucket.  */
  4732.  
  4733.   for (bp = symtab[hashval]; bp; bp = bp->link)
  4734.     if (! strcmp (key, bp->name))
  4735.       return bp;
  4736.  
  4737.   /* Nothing was found; create a new symbol table entry.  */
  4738.  
  4739.   bp = (symbol *) xmalloc (sizeof (symbol));
  4740.   bp->refs = 0;
  4741.   bp->name = (char *) xmalloc (strlen (key) + 1);
  4742.   strcpy (bp->name, key);
  4743.   bp->defined = 0;
  4744.   bp->referenced = 0;
  4745.   bp->trace = 0;
  4746.   bp->value = 0;
  4747.   bp->max_common_size = 0;
  4748.   bp->warning = 0;
  4749.   bp->undef_refs = 0;
  4750.   bp->multiply_defined = 0;
  4751.  
  4752.   /* Add the entry to the bucket.  */
  4753.  
  4754.   bp->link = symtab[hashval];
  4755.   symtab[hashval] = bp;
  4756.  
  4757.   ++num_hash_tab_syms;
  4758.  
  4759.   return bp;
  4760. }
  4761.  
  4762. /* Like `getsym' but return 0 if the symbol is not already known.  */
  4763.  
  4764. symbol *
  4765. getsym_soft (key)
  4766.      char *key;
  4767. {
  4768.   register int hashval;
  4769.   register symbol *bp;
  4770.  
  4771.   /* Determine which bucket.  */
  4772.  
  4773.   hashval = hash_string (key) % TABSIZE;
  4774.  
  4775.   /* Search the bucket.  */
  4776.  
  4777.   for (bp = symtab[hashval]; bp; bp = bp->link)
  4778.     if (! strcmp (key, bp->name))
  4779.       return bp;
  4780.  
  4781.   return 0;
  4782. }
  4783.  
  4784. /* Report a fatal error.
  4785.    STRING is a printf format string and ARG is one arg for it.  */
  4786.  
  4787. void
  4788. fatal (string, arg)
  4789.      char *string, *arg;
  4790. {
  4791.   fprintf (stderr, "ld: ");
  4792.   fprintf (stderr, string, arg);
  4793.   fprintf (stderr, "\n");
  4794.   exit (1);
  4795. }
  4796.  
  4797. /* Report a fatal error.  The error message is STRING
  4798.    followed by the filename of ENTRY.  */
  4799.  
  4800. void
  4801. fatal_with_file (string, entry)
  4802.      char *string;
  4803.      struct file_entry *entry;
  4804. {
  4805.   fprintf (stderr, "ld: ");
  4806.   fprintf (stderr, string);
  4807.   print_file_name (entry, stderr);
  4808.   fprintf (stderr, "\n");
  4809.   exit (1);
  4810. }
  4811.  
  4812. /* Report a fatal error using the message for the last failed system call,
  4813.    followed by the string NAME.  */
  4814.  
  4815. void
  4816. perror_name (name)
  4817.      char *name;
  4818. {
  4819.   extern int errno, sys_nerr;
  4820.   extern char *sys_errlist[];
  4821.   char *s;
  4822.  
  4823.   if (errno < sys_nerr)
  4824.     s = concat ("", sys_errlist[errno], " for %s");
  4825.   else
  4826.     s = "cannot open %s";
  4827.   fatal (s, name);
  4828. }
  4829.  
  4830. /* Report a fatal error using the message for the last failed system call,
  4831.    followed by the name of file ENTRY.  */
  4832.  
  4833. void
  4834. perror_file (entry)
  4835.      struct file_entry *entry;
  4836. {
  4837.   extern int errno, sys_nerr;
  4838.   extern char *sys_errlist[];
  4839.   char *s;
  4840.  
  4841.   if (errno < sys_nerr)
  4842.     s = concat ("", sys_errlist[errno], " for ");
  4843.   else
  4844.     s = "cannot open ";
  4845.   fatal_with_file (s, entry);
  4846. }
  4847.  
  4848. /* Report a nonfatal error.
  4849.    STRING is a format for printf, and ARG1 ... ARG3 are args for it.  */
  4850.  
  4851. void
  4852. error (string, arg1, arg2, arg3)
  4853.      char *string, *arg1, *arg2, *arg3;
  4854. {
  4855.   fprintf (stderr, "%s: ", progname);
  4856.   fprintf (stderr, string, arg1, arg2, arg3);
  4857.   fprintf (stderr, "\n");
  4858. }
  4859.  
  4860.  
  4861. /* Output COUNT*ELTSIZE bytes of data at BUF
  4862.    to the descriptor DESC.  */
  4863.  
  4864. void
  4865. mywrite (buf, count, eltsize, desc)
  4866.      char *buf;
  4867.      int count;
  4868.      int eltsize;
  4869.      int desc;
  4870. {
  4871.   register int val;
  4872.   register int bytes = count * eltsize;
  4873.  
  4874.   while (bytes > 0)
  4875.     {
  4876.       val = write (desc, buf, bytes);
  4877.       if (val <= 0)
  4878.     perror_name (output_filename);
  4879.       buf += val;
  4880.       bytes -= val;
  4881.     }
  4882. }
  4883.  
  4884. /* Output PADDING zero-bytes to descriptor OUTDESC.
  4885.    PADDING may be negative; in that case, do nothing.  */
  4886.  
  4887. void
  4888. padfile (padding, outdesc)
  4889.      int padding;
  4890.      int outdesc;
  4891. {
  4892.   register char *buf;
  4893.   if (padding <= 0)
  4894.     return;
  4895.  
  4896.   buf = (char *) alloca (padding);
  4897.   bzero (buf, padding);
  4898.   mywrite (buf, padding, 1, outdesc);
  4899. }
  4900.  
  4901. /* Return a newly-allocated string
  4902.    whose contents concatenate the strings S1, S2, S3.  */
  4903.  
  4904. char *
  4905. concat (s1, s2, s3)
  4906.      char *s1, *s2, *s3;
  4907. {
  4908.   register int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  4909.   register char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  4910.  
  4911.   strcpy (result, s1);
  4912.   strcpy (result + len1, s2);
  4913.   strcpy (result + len1 + len2, s3);
  4914.   result[len1 + len2 + len3] = 0;
  4915.  
  4916.   return result;
  4917. }
  4918.  
  4919. /* Parse the string ARG using scanf format FORMAT, and return the result.
  4920.    If it does not parse, report fatal error
  4921.    generating the error message using format string ERROR and ARG as arg.  */
  4922.  
  4923. int
  4924. parse (arg, format, error)
  4925.      char *arg, *format;
  4926. {
  4927.   int x;
  4928.   if (1 != sscanf (arg, format, &x))
  4929.     fatal (error, arg);
  4930.   return x;
  4931. }
  4932.  
  4933. /* Like malloc but get fatal error if memory is exhausted.  */
  4934.  
  4935. int
  4936. xmalloc (size)
  4937.      int size;
  4938. {
  4939.   register int result = malloc (size);
  4940.   if (!result)
  4941.     fatal ("virtual memory exhausted", 0);
  4942.   return result;
  4943. }
  4944.  
  4945. /* Like realloc but get fatal error if memory is exhausted.  */
  4946.  
  4947. int
  4948. xrealloc (ptr, size)
  4949.      char *ptr;
  4950.      int size;
  4951. {
  4952.   register int result = realloc (ptr, size);
  4953.   if (!result)
  4954.     fatal ("virtual memory exhausted", 0);
  4955.   return result;
  4956. }
  4957.  
  4958. #ifdef USG
  4959.  
  4960. void
  4961. bzero (p, n)
  4962.      char *p;
  4963. {
  4964.   memset (p, 0, n);
  4965. }
  4966.  
  4967. void
  4968. bcopy (from, to, n)
  4969.      char *from, *to;
  4970. {
  4971.   memcpy (to, from, n);
  4972. }
  4973.  
  4974. getpagesize ()
  4975. {
  4976.   return (4096);
  4977. }
  4978.  
  4979. #endif
  4980.  
  4981.  
  4982. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4983.  
  4984. static void
  4985. fix_byte_order(p, n)
  4986.     char *p;
  4987.     int n;
  4988. {
  4989.     char t;
  4990.  
  4991.     switch (n) {
  4992.  
  4993.     case 1:
  4994.     return;
  4995.  
  4996.     case 2:
  4997.     t = p[0];
  4998.     p[0] = p[1];
  4999.     p[1] = t;
  5000.     return;
  5001.  
  5002.     case 4:
  5003.     t = p[0];
  5004.     p[0] = p[3];
  5005.     p[3] = t;
  5006.     t = p[1];
  5007.     p[1] = p[2];
  5008.     p[2] = t;
  5009.     return;
  5010.  
  5011.     default:
  5012.     fatal("Internal Error in fix_byte_order, n = %d\n", 0);
  5013.     }
  5014. }
  5015.  
  5016. static void
  5017. fix_exec_header_byte_order(execP)
  5018.     struct exec *execP;
  5019. {
  5020.  
  5021. #if TARGET_MACHINE==TARGET_SUN4
  5022.     if (N_BADMAG(*execP)) {
  5023.     u_char c = * (u_char *) execP;
  5024.     execP->a_dynamic = c & 1;
  5025.     execP->a_toolversion = c >> 1;
  5026.     } else {
  5027.     u_char c = execP->a_toolversion | (execP->a_dynamic << 7);
  5028.     * (u_char *) execP = c;
  5029.     }
  5030. #elif TARGET_MACHINE!=TARGET_SEQUENT
  5031.     fix_byte_order(&execP->a_machtype, sizeof(execP->a_machtype));
  5032. #endif
  5033.     fix_byte_order(&execP->a_magic,    sizeof(execP->a_magic));
  5034.     fix_byte_order(&execP->a_text,     sizeof(execP->a_text));
  5035.     fix_byte_order(&execP->a_data,     sizeof(execP->a_data));
  5036.     fix_byte_order(&execP->a_bss,      sizeof(execP->a_bss));
  5037.     fix_byte_order(&execP->a_syms,     sizeof(execP->a_syms));
  5038.     fix_byte_order(&execP->a_entry,    sizeof(execP->a_entry));
  5039.     fix_byte_order(&execP->a_trsize,   sizeof(execP->a_trsize));
  5040.     fix_byte_order(&execP->a_drsize,   sizeof(execP->a_drsize));
  5041.     return;
  5042. }
  5043.  
  5044. static void
  5045. fix_symbol_byte_order(p, size)
  5046.     struct nlist *p;
  5047.     int size;
  5048. {
  5049.     int n;
  5050.  
  5051.     for (n = size / sizeof(*p); --n >= 0; ++p) {
  5052.     fix_byte_order(&p->n_un, sizeof(p->n_un));
  5053.     fix_byte_order(&p->n_desc, sizeof(p->n_desc));
  5054.     fix_byte_order(&p->n_value, sizeof(p->n_value));
  5055.     }
  5056.     return;
  5057. }
  5058.  
  5059. static void
  5060. target_to_host_reloc_byte_order(relocP, nrelocs)
  5061.     struct relocation_info *relocP;
  5062.     int nrelocs;
  5063. {
  5064.     u_char c[4];
  5065.  
  5066. #if TARGET_BYTE_ORDER==BIG_ENDIAN && HOST_BYTE_ORDER==LITTLE_ENDIAN
  5067.     for (; --nrelocs >= 0; ++relocP) {
  5068.     fix_byte_order(&relocP->r_address, sizeof(relocP->r_address));
  5069.     c[0] = ((u_char *) relocP)[4];
  5070.     c[1] = ((u_char *) relocP)[5];
  5071.     c[2] = ((u_char *) relocP)[6];
  5072.     c[3] = ((u_char *) relocP)[7];
  5073.     ((u_long *) relocP)[1] = 0;
  5074. #if TARGET_MACHINE==TARGET_SUN4
  5075.     relocP->r_index = (c[0] << 16) | (c[1] << 8) | c[2];
  5076.     relocP->r_type = (enum reloc_type) (c[3] & 0x1f);
  5077.     relocP->r_extern = (c[3] & 0x80) >> 7;
  5078.     fix_byte_order(&relocP->r_addend, sizeof(relocP->r_addend));
  5079. #else
  5080.     relocP->r_symbolnum = (c[0] << 16) | (c[1] << 8) | c[2];
  5081.     relocP->r_pcrel = (c[3] >> 7) & 1;
  5082.     relocP->r_length = (c[3] >> 5) & 3;
  5083.     relocP->r_extern = (c[3] >> 4) & 1;
  5084. #endif
  5085.     }
  5086. #endif
  5087.     return;
  5088. }
  5089.  
  5090. static void
  5091. host_to_target_reloc_byte_order(relocP, nrelocs)
  5092.     struct relocation_info *relocP;
  5093.     int nrelocs;
  5094. {
  5095.     u_char c[4];
  5096.  
  5097. #if TARGET_BYTE_ORDER==BIG_ENDIAN && HOST_BYTE_ORDER==LITTLE_ENDIAN
  5098.     for (; --nrelocs >= 0; ++relocP) {
  5099.     fix_byte_order(&relocP->r_address, sizeof(relocP->r_address));
  5100. #if TARGET_MACHINE==TARGET_SUN4
  5101.     c[0] = (relocP->r_index >> 16);
  5102.     c[1] = (relocP->r_index >> 8);
  5103.     c[2] = relocP->r_index;
  5104.     c[3] = (u_char) relocP->r_type | (relocP->r_extern << 7);
  5105.     fix_byte_order(&relocP->r_addend, sizeof(relocP->r_addend));
  5106. #else
  5107.     c[0] = (relocP->r_symbolnum >> 16);
  5108.     c[1] = (relocP->r_symbolnum >> 8);
  5109.     c[2] = relocP->r_symbolnum;
  5110.     c[3] = (relocP->r_pcrel << 7) |
  5111.         (relocP->r_length << 5) | (relocP->r_extern << 4);
  5112. #endif
  5113.     ((u_char *) relocP)[4] = c[0];
  5114.     ((u_char *) relocP)[5] = c[1];
  5115.     ((u_char *) relocP)[6] = c[2];
  5116.     ((u_char *) relocP)[7] = c[3];
  5117.     }
  5118. #endif
  5119.     return;
  5120. }
  5121.  
  5122. static void
  5123. fix_symbol_root_byte_order(p)
  5124.     struct symbol_root *p;
  5125. {
  5126.  
  5127.     fix_byte_order(&p->format, sizeof(p->format));
  5128.     fix_byte_order(&p->length, sizeof(p->length));
  5129.     fix_byte_order(&p->ldsymoff, sizeof(p->ldsymoff));
  5130.     fix_byte_order(&p->textrel, sizeof(p->textrel));
  5131.     fix_byte_order(&p->datarel, sizeof(p->datarel));
  5132.     fix_byte_order(&p->bssrel, sizeof(p->bssrel));
  5133.     fix_byte_order(&p->filename, sizeof(p->filename));
  5134.     fix_byte_order(&p->filedir, sizeof(p->filedir));
  5135.     fix_byte_order(&p->blockvector, sizeof(p->blockvector));
  5136.     fix_byte_order(&p->typevector, sizeof(p->typevector));
  5137.     fix_byte_order(&p->language, sizeof(p->language));
  5138.     fix_byte_order(&p->version, sizeof(p->version));
  5139.     fix_byte_order(&p->compilation, sizeof(p->compilation));
  5140.     fix_byte_order(&p->databeg, sizeof(p->databeg));
  5141.     fix_byte_order(&p->bssbeg, sizeof(p->bssbeg));
  5142.     fix_byte_order(&p->sourcevector, sizeof(p->sourcevector));
  5143.     return;
  5144. }
  5145.  
  5146. #endif
  5147.  
  5148.